blogs.conchango.com

welcome to the conchango blogging site
Welcome to blogs.conchango.com Sign in | Join | Help
in Search

Jim 2.0

T-SQL: Common date ranges; Daily, Weekly, Monthly, Yearly, Last 12 Months

As promised in my original post on PL/SQL date ranges previously, below is a sql statement which can be used preceeding most queries in order to apply common date range lookups to the data the query returns. SQL Server Management Studio (SSMS) doesn't have an equivalent to the '&' functionality that TOAD has to prompt for variables, however what it does allow you to do, which I find particularly useful, is to declare and specify all of your variables upfront.

In order to make the statement more foolproof, it is designed so the user only has to enter the date view and general date of interest and the specific start and end dates are derived. For example, if date view is 'Monthly', the user need only enter the Month and Year, e.g. December 2006. The statement however requires dates be entered in an unambiguous format to make it even more robust, so December 2006 must actually be entered as '200612'.

Copy and paste the statement below into a new query in SSMS and run it to experiment with different variables. Refer to the comments for guidance on what variables are allowed. Once again enddate is derived to take account of the current date, such as if year is the current year, etc.

    1 DECLARE  @DateView       varchar (20)

    2 ,        @theDate        varchar (20)

    3 ,        @startDate      datetime

    4 ,        @endDate        datetime

    5 

    6 SELECT   @DateView       =  'Yearly'

    7 --enter the date view required: Select from 'Daily', 'Weekly', 'Monthly', 'Yearly' and 'Last 12 Months' or 'All'

    8 ,        @thedate        =  '2007'

    9 --For Daily enter the date in the format 'YYYYMMDD' - e.g. '20060901' for September 1st 2006

   10 --For Weekly enter the week ending date in the format 'YYYYMMDD'. Note that the week ends on a Sunday - e.g. '20060910' for September 10th 2006

   11 --For Monthly enter date in the format 'YYYYMM' - e.g. '200609' for September 2006

   12 --For Yearly enter the date in the format 'YYYY' - e.g. '2006'

   13 --For Last 12 Months enter the date is not required as it is calculated up to the current date

   14 --Do not update startdate and enddate below:

   15 ,        @startDate      =  case

   16                             when @DateView = 'Daily'

   17                             then cast(@thedate as datetime)

   18                             when @DateView = 'Weekly'

   19                             then dateadd(dd,-6,cast(@thedate as datetime))

   20                             when @DateView = 'Monthly'

   21                             then cast(@thedate+'01' as datetime)

   22                             when @DateView = 'Yearly'

   23                             then cast(@thedate as datetime)

   24                             when @DateView = 'Last 12 Months'

   25                             then dateadd(mm,-11,cast(cast(datepart(yyyy,getdate()) as varchar(4))+right('0'+cast(datepart(mm,getdate()) as varchar(2)),2) + '01' as datetime))

   26                             else cast('2005-01-01' as datetime)

   27                             end

   28 ,        @endDate        =  case

   29                             when @DateView = 'Daily'

   30                             then dateadd(dd, 1, @startDate)

   31                             when @DateView = 'Weekly'

   32                             then (

   33                                  case when cast(@thedate as datetime)+1 > getdate()

   34                                  then cast(CONVERT(char(10), getdate(), 110) as datetime)

   35                                  else cast(@thedate as datetime)+1

   36                                  end

   37                                  )

   38                             when @DateView = 'Monthly'

   39                             then (

   40                                  case when dateadd(mm, 1, @startDate) > getdate()

   41                                  then cast(CONVERT(char(10), getdate(), 110) as datetime)

   42                                  else dateadd(mm, 1, @startDate)

   43                                  end

   44                                  )

   45                             when @DateView = 'Yearly'

   46                             then (

   47                                  case when dateadd(mm,12,@StartDate) > getdate()

   48                                  then cast(CONVERT(char(10), getdate(), 110) as datetime)

   49                                  else dateadd(mm,12,@StartDate)

   50                                  end

   51                                  )

   52                             when @DateView = 'Last 12 Months'

   53                             then cast(CONVERT(char(10), getdate(), 110) as datetime)

   54                             else cast(CONVERT(char(10), getdate(), 110) as datetime)

   55                             end

   56 

   57 print    @thedate

   58 print    @startdate

   59 print    @enddate

One of the big advantages of this method is that only one query is required to easily return data for any of the common date ranges, because of the use of the date view variable. To use this, simply paste it above the query desired (without the print commands), and use @startdate and @enddate in the where clauses, for example:

    1 select  *

    2 from    a_table

    3 where   a_date >= @startdate

    4 and     a_date <  @enddate

James

Published 01 March 2007 00:34 by James.Pipe

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

 

jamie.thomson said:

Hiya mate,

One small thing. I doubt that '&' is actually proper PL/SQL syntax. Its a placeholder that can be interpreted by the query tool (i.e. TOAD) which will be replaced by whatever literal you supply when prompted. The difference is subtle but important.

If you sent an ampersand to the Oracle query processor then it would (I expect) fail.

I stand to be corrected however cos I know next to nowt about PL/SQL.

-Jamie

March 1, 2007 02:24
 

Jim 2.0 said:

Recently my colleague challenged me to find a method for identifying the week ending date for the week

April 24, 2007 15:46

Leave a Comment

(required) 
(optional)
(required) 
Submit
Powered by Community Server (Personal Edition), by Telligent Systems