The arguments for DATEADD are DATEADD(interval, increment int, expression smalldatetime). So in the expression above, the DATEDIFF function returns an integer, being the difference in years between the current date and date zero (0). The expression in the above statement accepts a zero (0) as a smalldatetime.
This works by figuring out the number of years since 1900 with DATEDIFF(yy, 0, GETDATE()) and then adding that to a date of zero = Jan 1, 1900. This can be changed to work for an arbitrary date by replacing the GETDATE() portion or an arbitrary year by replacing the DATEDIFF(...) function with "Year - 1900."
1. First day of current month: select DATEADD(mm, DATEDIFF(m,0,GETDATE()),0): in this we have taken out the difference between the months from 0 to current date and then add the difference in 0 this will return the first day of current month.
SELECT
DATEADD(yy, DATEDIFF(yy, 0, GETDATE()), 0) AS StartOfYear,
DATEADD(yy, DATEDIFF(yy, 0, GETDATE()) + 1, -1) AS EndOfYear
The above query gives a datetime value for midnight at the beginning of December 31. This is about 24 hours short of the last moment of the year. If you want to include time that might occur on December 31, then you should compare to the first of the next year, with a <
comparison. Or you can compare to the last few milliseconds of the current year, but this still leaves a gap if you are using something other than DATETIME (such as DATETIME2):
SELECT
DATEADD(yy, DATEDIFF(yy, 0, GETDATE()), 0) AS StartOfYear,
DATEADD(yy, DATEDIFF(yy, 0, GETDATE()) + 1, -1) AS LastDayOfYear,
DATEADD(yy, DATEDIFF(yy, 0, GETDATE()) + 1, 0) AS FirstOfNextYear,
DATEADD(ms, -3, DATEADD(yy, DATEDIFF(yy, 0, GETDATE()) + 1, 0)) AS LastTimeOfYear
Other Periods
This approach has two nice aspects: good performance and it can easily be changed for other periods by replacing both occurrences of yy
(=year) with a different string:
yy, yyyy year
qq, q quarter
mm, m month
wk, ww week
(Be careful of weeks: the starting day depends on server settings.)
Tech Details
This works by figuring out the number of years since 1900 with DATEDIFF(yy, 0, GETDATE())
and then adding that to a date of zero = Jan 1, 1900. This can be changed to work for an arbitrary date by replacing the GETDATE()
portion or an arbitrary year by replacing the DATEDIFF(...)
function with "Year - 1900."
SELECT
DATEADD(yy, DATEDIFF(yy, 0, '20150301'), 0) AS StartOfYearForMarch2015,
DATEADD(yy, 2015 - 1900, 0) AS StartOfYearFor2015
Here's a fairly simple way;
SELECT DATEFROMPARTS(YEAR(GETDATE()), 1, 1) AS 'First Day of Current Year';
SELECT DATEFROMPARTS(YEAR(GETDATE()), 12, 31) AS 'End of Current Year';
It's not sexy, but it works.
You can get the current year using DATEPART
function, from the current date obtained using getUTCDate()
SELECT
'01/01/' + CONVERT(VARCHAR(4), DATEPART(yy, getUTCDate())),
'31/12/' + CONVERT(VARCHAR(4), DATEPART(yy, getUTCDate()))
simply write:-
select convert (date,DATEADD(YEAR,DATEDIFF(YEAR,0,GETDATE()),0))
start date of the year.
select convert (date,DATEADD(YEAR, DATEDIFF(YEAR,0,GETDATE()) + 1, -1))
Every year has the 1 st as First date and 31 as the last date what you have to do is only attach the year to that day and month for example:-
SELECT '01/01/'+cast(year(getdate()) as varchar(4)) as [First Day],
'12/31/'+cast(year(getdate()) as varchar(4)) as [Last Day]
To get the first and the last day of the year, one can use the CONCAT
function. The resulting value may be cast to any type.
CONCAT(YEAR(Getdate()),'-01-01') FirstOfYear,
CONCAT(YEAR(GETDATE()),'-12-31') LastOfYear
Another way: (Since SQL Server 2012)
SELECT
DATEFROMPARTS(YEAR(GETDATE()), 1, 1) FirstDay,
DATEFROMPARTS(YEAR(GETDATE()),12,31) LastDay
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With