DECLARE
@curDate
DATE
=
CURRENT_TIMESTAMP
;
@YearStart
= dateadd (
year
,datediff(
,
'19000101'
, @curDate)
);
SELECT
datediff(
day
, @YearStart, @curDate) + 1
AS
[Number
of
Days
from
the
Year
Start]
datepart(dayofyear,
current_timestamp
)
Days]
@MinDate DATETIME, @MaxDate DATETIME;
SET
@MinDate = DATEADD(
month
, DATEDIFF(
'19000201'
),
@MaxDate = DATEADD(
,-1,DATEADD(
, 1, @MinDate))
-- for versions prior to SQL 2012;
@MaxDate = EOMONTH(@MinDate);
-- for SQL Server 2012 and up
Naomi N edited Revision 1. Comment: Added Gert-Jan code
Naomi N edited Revision 2. Comment: Formatting
Naomi N edited Revision 3. Comment: Adjusted link
Naomi N edited Revision 4. Comment: Added tag
Ed Price - MSFT edited Revision 5. Comment: Title casing and clarified technology in the title. Feel free to refine. Great article, and great presentation of the code!
Naomi N edited Revision 6. Comment: Added day for the today link
Naomi N edited Revision 8. Comment: Added toc - need to add more cases
Naomi N edited Revision 9. Comment: Added tense, link and new tag
Naomi N edited Revision 10. Comment: Added See Also
Naomi N edited Revision 13. Comment: Added link
Congratulations. Nice, brief and to-the-point, but also rich enough in detail to follow easily.
In SQL Server 2012 you can program as:
SELECT DATEDIFF(dd, EOMONTH(current_timestamp,-MONTH(current_timestamp)), current_timestamp);