Monday 20 April 2015

How many Days in a year by SQL Query

SQL Server How many Days in a year by query :

DECLARE @year AS INT
SET @year=2016
SELECT DATEPART(dy,(DATEADD(YEAR,@year-1899,0)-1))
AS [TOTAL NO OF DAYS]

Output :366

Easy to understand the query by splitting as below
Subtract the year 2016 in the below query as
select DATEADD(YEAR,2016-1899,0)

Then the query will be below

select DATEADD(YEAR,117,0)
Output :
2017-01-01 00:00:00.000

select DATEPART(dy, ‘2017-01-01’-1)

Then the query will be below
select DATEPART(dy,'2016/12/31')

Output : 366

No comments:

Post a Comment