How to check leap year or not in sql server

Many times in the interviewer ask the question how to check which year is leap years or not in SQL Server

Below this method we can check the which year this leap years or not


 SqlQuery:-
DECLARE @year int

SET @year = 2016

SELECT CASE WHEN (
 (@year % 4 = 0) and (@year % 100 != 0) or

        (@year % 400 = 0)
)
then 'YES' else 'NO'
END
AS [Is Leap year?]
 
Note :-
If we want to check current then we need to put :-
SET @year =  YEAR(getdate())

Out-Put:-





How to check leap year or not in sql server How to check leap year or not in sql server Reviewed by NEERAJ SRIVASTAVA on 6:50:00 PM Rating: 5

No comments:

Powered by Blogger.