Database Journal
MS SQL Oracle DB2 Access MySQL PostgreSQL Sybase PHP SQL Etc SQL Scripts & Samples Links Database Forum

» Database Journal Home
» Database Articles
» Database Tutorials
MS SQL
Oracle
DB2
MS Access
MySQL
» RESOURCES
Database Tools
SQL Scripts & Samples
Links
» Database Forum
» Sitemap
Free Newsletters:
DatabaseDaily  
News Via RSS Feed


follow us on Twitter
Database Journal |DBA Support |SQLCourse |SQLCourse2
 

Featured Database Articles

SQL Scripts & Samples

Posted Apr 19, 2004

PreviousQuarter

By DatabaseJournal.com Staff



>>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



SQL Scripts & Samples Archives

Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 




Latest Forum Threads
SQL Scripts & Samples Forum
Topic By Replies Updated
sql help!! add 1 August 22nd, 11:58 AM
How To Combine These 2 Queries into 1 Query ? tarek_land 1 June 7th, 08:37 AM
solving query svibuk 1 February 3rd, 06:08 AM
converting from a character string to uniqueidentifier saturnius 4 January 4th, 05:56 AM


















Thanks for your registration, follow us on our social networks to keep up-to-date