PreviousQuarter

>>Script Language and Platform: SQL Server 2000
This script will return you to the first day of previous quarter of a given date.

–today’s date Apr 19, 2004
select dbo.previousquarter (getdate())
–returns 2004-01-01 00:00:00.000

select dbo.previousquarter (’03/31/2004′)
–returns 2003-10-01 00:00:00.000

select dbo.previousquarter (’04/01/2004′)
–returns 2004-01-01 00:00:00.000

select dbo.previousquarter (’07/11/2004′)
–returns 2004-04-01 00:00:00.000

select dbo.previousquarter (’09/09/1974′)
–returns 1974-04-01 00:00:00.000

Author: The MAK



Create function dbo.previousquarter (@date datetime)
returns datetime
as begin
–Author -MAK
–Objective– To return the first day of previous quarter of a given date
declare @month tinyint
declare @year int
declare @returndate datetime
if month(@date) between 1 and 3 begin set @month=10 set @year=year(@date)-1 end
if month(@date) between 4 and 6 begin set @month=1 set @year=year(@date) end
if month(@date) between 7 and 9 begin set @month=4 set @year=year(@date) end
if month(@date) between 10 and 12 begin set @month=7 set @year=year(@date) end
–print @month
–print @year
set @returndate = convert(datetime,right(’00’+convert(varchar(2),@month),2)
+’/01/’+convert(varchar(4),@year))
return @returndate

end



Disclaimer:
We hope that the information on these script pages is
valuable to you. Your use of the information contained in these pages,
however, is at your sole risk. All information on these pages is provided
“as -is”, without any warranty, whether express or implied, of its accuracy,
completeness, or fitness for a particular purpose…

Disclaimer Continued

Back to Database Journal Home

Get the Free Newsletter!

Subscribe to Cloud Insider for top news, trends & analysis

Latest Articles