Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

How to first date of the previous Quater of the given date. Expand / Collapse
Author
Message
Posted Wednesday, January 9, 2013 2:44 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Yesterday @ 2:52 AM
Points: 134, Visits: 258
Hi,
I am using Sql server 2008,
My requirement is to find the first date of the previous Quater of the given date.

Example,

Givendate Quater Required Output

2013-01-20 1 2012-10-01

2013-03-20 1 2012-10-01

2013-04-10 2 2013-01-01

2012-10-10 4 2012-07-01


Please Help me,
As I need this very urgently.

Thanks in Advance!





Post #1404621
Posted Wednesday, January 9, 2013 2:53 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Today @ 2:10 AM
Points: 5,216, Visits: 5,108
I would say this is where a calendar table like this one will come into best fit. http://www.sqlservercentral.com/scripts/Date/68389/

Then you can query it to find todays quarter, then lookup the previous quarters first day.





Want an answer fast? Try here
How to post data/code for the best help - Jeff Moden
Need a string splitter, try this - Jeff Moden
How to post performance problems - Gail Shaw
CrossTabs-Part1 & Part2 - Jeff Moden
SQL Server Backup, Integrity Check, and Index and Statistics Maintenance - Ola Hallengren
Managing Transaction Logs - Gail Shaw
Troubleshooting SQL Server: A Guide for the Accidental DBA - Jonathan Kehayias and Ted Krueger

Post #1404626
Posted Wednesday, January 9, 2013 3:54 AM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Wednesday, September 17, 2014 9:19 AM
Points: 1,307, Visits: 4,508
I agree with Anthony... a calendar table is probably a better idea, but this should get you the first day of the previous quarter.

declare @d datetime
set @d = getdate() -- or whatever date you want to test

select DATEADD(quarter,DATEDIFF(quarter,0,@d)-1,0)




Post #1404657
Posted Wednesday, January 9, 2013 4:07 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Yesterday @ 2:52 AM
Points: 134, Visits: 258
Thanks Friends!



It works for me.
Post #1404662
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse