Wednesday, 23 July 2014

Get Date Month and Year using Sql Server

Ans:-
--get first of date and month of the year
select DATEADD(m, 0, DATEADD(yyyy, 2014 - 1900, 0))
--get last of date and month of the year
select DATEADD(d, -1, DATEADD(m, 12, DATEADD(yyyy, 2014 - 1900, 0)))
--get previous year of last date and month
select DATEADD(d, -1, DATEADD(m, 12, DATEADD(yyyy, 2014 - 1901, 0)))

--get first date of month of the year
select DATEADD(m, 7 - 1, DATEADD(yyyy, 2014 - 1900, 0))
--get last date of month of the year                
select DATEADD(d, -1, DATEADD(m, 7, DATEADD(yyyy, 2014 - 1900, 0)))
--get previous month last date of month of the year                
select DATEADD(d, -1, DATEADD(m, 7-1, DATEADD(yyyy, 2014 - 1900, 0)))

No comments:

Post a Comment