SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 Script Library
 Date conversions
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

DonAtWork
Flowing Fount of Yak Knowledge

2161 Posts

Posted - 09/12/2012 :  10:34:19  Show Profile  Reply with Quote
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

USA
15663 Posts

Posted - 09/12/2012 :  10:51:10  Show Profile  Visit robvolk's Homepage  Reply with Quote
Why not just use SELECT DATEADD(q, 0 + DATEDIFF(q, 0, @d), 0) ?
Go to Top of Page

DonAtWork
Flowing Fount of Yak Knowledge

2161 Posts

Posted - 09/12/2012 :  11:06:10  Show Profile  Reply with Quote
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

USA
794 Posts

Posted - 09/12/2012 :  13:44:26  Show Profile  Reply with Quote
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

India
22754 Posts

Posted - 09/13/2012 :  02:04:56  Show Profile  Send madhivanan a Yahoo! Message  Reply with Quote
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

India
22754 Posts

Posted - 09/13/2012 :  02:10:47  Show Profile  Send madhivanan a Yahoo! Message  Reply with Quote
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
Flowing Fount of Yak Knowledge

2161 Posts

Posted - 09/14/2012 :  06:51:54  Show Profile  Reply with Quote
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

India
22754 Posts

Posted - 09/14/2012 :  07:21:53  Show Profile  Send madhivanan a Yahoo! Message  Reply with Quote
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
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.09 seconds. Powered By: Snitz Forums 2000