Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 Script Library
 Date conversions

Author  Topic 

DonAtWork
Master Smack Fu Yak Hacker

2167 Posts

Posted - 2012-09-12 : 10:34:19
I was messing with some dates, and found a quick little bit that changed a date to the 1st day of the month:
declare @d date = '11/23/2012'
SELECT DATEADD(mm, 0 + DATEDIFF(mm, 0, @d), 0)


Naturally, someone now needed a date converted to the 1st day of a quarter. This little algorithm assumes that quarters start Jan 1st.
SELECT DATEADD(mm, - CASE 
WHEN (MONTH(DATEADD(mm, 0 + DATEDIFF(mm, 0, @d), 0) )% 3) > 0
THEN ((MONTH(DATEADD(mm, 0 + DATEDIFF(mm, 0, @d), 0) )% 3) -1)
ELSE 2 END
,DATEADD(mm, 0 + DATEDIFF(mm, 0, @d), 0)
)


Any suggestions or comments would be appreciated.








How to ask: http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx

robvolk
Most Valuable Yak

15732 Posts

Posted - 2012-09-12 : 10:51:10
Why not just use SELECT DATEADD(q, 0 + DATEDIFF(q, 0, @d), 0) ?
Go to Top of Page

DonAtWork
Master Smack Fu Yak Hacker

2167 Posts

Posted - 2012-09-12 : 11:06:10
umm.. because that is way too readable?








How to ask: http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx

Go to Top of Page

jeffw8713
Aged Yak Warrior

819 Posts

Posted - 2012-09-12 : 13:44:26
You can simplify both of your equations by removing the '0 +' before the DATEDIFF.

SELECT DATEADD(month, DATEDIFF(month, 0, GETDATE()), 0);

For a little variation:

DECLARE @numMonths int;
SET @numMonths = -3;

SELECT DATEADD(month, DATEDIFF(month, 0, GETDATE()) - @numMonths), 0);
SELECT DATEADD(month, DATEDIFF(month, -1, GETDATE()) - @numMonths), -1);

Swap out month for any other date interval and see what happens.

Jeff
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2012-09-13 : 02:04:56
Go to the last section of this post and find more such methods
http://beyondrelational.com/modules/2/blogs/70/posts/10899/understanding-datetime-column-part-iii.aspx

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2012-09-13 : 02:10:47
quote:
Originally posted by DonAtWork

umm.. because that is way too readable?








How to ask: http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx




Thats a good excuse

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

DonAtWork
Master Smack Fu Yak Hacker

2167 Posts

Posted - 2012-09-14 : 06:51:54
quote:
Originally posted by madhivanan

Go to the last section of this post and find more such methods
http://beyondrelational.com/modules/2/blogs/70/posts/10899/understanding-datetime-column-part-iii.aspx

Madhivanan

Failing to plan is Planning to fail



LOVE that post. Very useful!








How to ask: http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx

Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2012-09-14 : 07:21:53
quote:
Originally posted by DonAtWork

quote:
Originally posted by madhivanan

Go to the last section of this post and find more such methods
http://beyondrelational.com/modules/2/blogs/70/posts/10899/understanding-datetime-column-part-iii.aspx

Madhivanan

Failing to plan is Planning to fail



LOVE that post. Very useful!








How to ask: http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx




Thanks

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -