Transact-SQL: Date-Related Queries (featured article)
Today we're featuring an article from mult-gold-medal winner of the T-SQL Guru competition, multi-winner of the MCC award, and TechNet Wiki Community Council member, Naomi N:
Thank you to Naomi for providing this great resource:
Here is an excerpt from the article:
Finding Day Number from the Beginning of the Year
I want to start with this simple question that was posted today (May 31, 2013) - how to find today's date day number from the beginning of the year.
This is my solution and a bit of explanation at the end
DECLARE @curDate ``DATE = ``CURRENT_TIMESTAMP``;
DECLARE @YearStart ``DATE= dateadd (
year
,datediff(``year`` , ``'19000101'``, @curDate)
,``'19000101'
);
SELECTdatediff(``day`` , @YearStart, @curDate) + 1 ``AS [Number ``of Days ``from the ``Year Start]
=====================
Thanks again to Naomi!
Check out the full article here:
- User Ed