Skip to main content

Finding Leap Year using SQL



Before we start writing a statement to find a Leap Year, let us first understand what is a Leap Year.

According to Gregorian Calendar each year consists of 365 day. In reality Earth takes approximately 365.242375 days to circle once around the Sun.

As the Gregorian calendar has only 365 days in a year, so if we didn't add a day on February 29 nearly every 4 years, we would lose almost six hours off our calendar every year. After only 100 years, our calendar would be off by approximately 24 days!

Lets look at it in more details,

Because the Earth rotates about 365.242375 times a year ...

... but a normal year is 365 days, ...

... so something has to be done to "catch up" the extra 0.242375 days a year.

So every 4th year we add an extra day (the 29th of February), which makes 365.25 days a year. This is fairly close, but is wrong by about 1 day every 100 years.

So every 100 years we don't have a leap year, and that results in 365.24 days per year (1 day less in 100 year = -0.01 days per year). Closer, but still not accurate enough!

So another rule says that every 400 years is a leap year again. This gets us 365.2425 days per year (1 day regained every 400 years = 0.0025 days per year), which is close enough to 365.242375 not to matter much.

So this is why we have Leap Year once every 4 years.

According to Gregorian calendar 3 criteria must be taken into account to identify leap years:

1. The year is evenly divisible by 4;
2. If the year can be evenly divided by 100, it is NOT a leap year, unless;
3. The year is also evenly divisible by 400. Then it is a leap year.

This means that 2000 and 2400 are leap years, while 1800, 1900, 2100, 2200, 2300 and 2500 are NOT leap years.

The year 2000 was somewhat special as it was the first instance when the third criterion was used in most parts of the world since the transition from the Julian to the Gregorian Calendar.


Now its time for an SQL which can check all 3 conditions.

Following example is with CASE conditional Expression,

SELECT 
CASE WHEN (mod(&&a,4) = 0 and mod(&a,100) <> 0) OR
(mod(&a,4) = 0 and mod(&a,100) = 0 and mod(&a,400) = 0)
THEN '&&a is a Leap Year'
ELSE '&a is not a Leap Year' END
FROM dual;

Following example is without CASE,

SELECT '&&a is a Leap Year'
FROM dual
WHERE (mod(&a,4) = 0 and mod(&a,100) <> 0)
OR
(mod(&a,4) = 0 and mod(&a,100) = 0 and mod(&a,400) = 0);





Comments

Unknown said…
Hi Mithun,
When we execute this statement for second time it is directly giving the same output of the previous input i.e once the variable given a value or initialized we need to take another variable name and execute the statement for another input; Is there any solution that we can give different input for the same variable.
Regards
Chandra
Mithun said…
Hi Chandra,

Once you execute the query to set the variable again, execute the command below,

undef a
Unknown said…
thank u Mithun ...
Unknown said…
hello mithun..
I need a query to display my current age .... so my birth date is 16th sep 1988...
Thank you..
Unknown said…
This comment has been removed by the author.
Unknown said…
select trunc(months_between(sysdate,'16-sep-1988')/12) years from dual

Popular posts from this blog

SQL Interview Questions on Subqueries

SUB Queries:
1. List the employees working in research department 2. List employees who are located in New York and Chicago
3. Display the department name in which ANALYSTS are working
4. Display employees who are reporting to JONES
5. Display all the employees who are reporting to Jones Manager
6. Display all the managers in SALES and ACCOUNTING department
7. Display all the employee names in Research and Sales Department who are having at least 1 person reporting to them
8. Display all employees who do not have any reportees
9. List employees who are having at least 2 reporting
10. List the department names which are having more than 5 employees
11. List department name having at-least 3 salesman
12. List employees from research and accounting having at-least 2 reporting
13. Display second max salary
14. Display 4th max salary
15. Display 5th max salary  -- Answer for nth Max Salary
Co-Related Subqueries:
16. Write a query to get 4th max salary from EMP table
17. Write a query to get 2nd…

Basics of RDBMS

Data
Small set of information becomes data, this set of information helps make decision. Data is always some useful information.


Database
Place where you store the data. Database represents some aspect of the real world called "miniworld". A database is designed, built and populated with data for a specific purpose. It has intended group of users and some preconceived applications in which these users are interested.

In other words, a database has some source from which data is derived, some degree of interaction with events in the real world and an audience that is actively interested in the contents of the database.

Database can also be defined as collection of one or more tables.

Ex: Mobile, human brain etc



DBMS (Database Management System)
Is a program that stores retrieves and modifies data in the database on request.

Study of different techniques of design, development and maintenance of the database

Types of DBMS
These types are based upon their management of database s…

Answers for SQL Functions

1. SQL> SELECT empno, ename FROM emp WHERE Length(ename) = 4;

2. SQL> SELECT empno, ename, job FROM emp where Length(job)=7;

3. SQL> SELECT Length('qspiders') - Length(replace('qspiders','s','')) FROM dual;

4. SQL>  SELECT empno, ename, job FROM emp WHERE Instr(job,'MAN') >0;

5. SQL> SELECT empno, ename, job FROM emp WHERE Instr(job, 'MAN') =1;

6. SQL> SELECT empno, ename, job FROM emp WHERE (Length(ename) - Length(Replace(ename, 'L',''))) = 1;

7. SQL> SELECT * FROM dept WHERE Instr(dname,'O') > 0;

8. SQL> SELECT Concat(ename,' working as a ') || Concat(job, ' earns ') || Concat(sal, '  in ') || Conc
at('dept ',deptno) AS text from emp;

OR

SQL> SELECT Concat(Concat(Concat(Concat(Concat(Concat(Concat(ename,' working as a '), job),' earns '), sal),'  in '),'dept '), deptno) AS text FROM emp;

9. SQL> SELECT empno, ename…