Solved

If then in where clause

Posted on 2015-01-23
7
100 Views
Last Modified: 2015-02-01
I am trying to write a where clause if the month of today's date is = 1 then the date range is the beginning for the past year to the end of the past year, else it is the beginning of the current year to the end of last month.

Here is what I have now:

(dbo.OeOrders.OrderDateTime >= CAST(year(getdate())-1 as varchar(4))+'0101'  AND      dbo.OeOrders.OrderDateTime >= CAST(year(getdate()) as varchar(4))+'0101') 

Open in new window


Thanks

Glen
0
Comment
Question by:GPSPOW
  • 2
  • 2
  • 2
  • +1
7 Comments
 
LVL 65

Expert Comment

by:Jim Horn
ID: 40567256
So something like ...
WHERE 
  (SomeDate BETWEEN {beginning of last year} AND {end of last year} AND DATEPART(month, GETDATE()) = 1) OR 
  (SomeDate BETWEEN {beginning of currentyear} AND {end of last month} AND DATEPART(month, GETDATE()) <> 1)

Open in new window


btw if you had a Calendar Table in SQL Server, then you could join on the dates, do some math, and get all of the date boundaries you need, without having to use huge expressions.
0
 

Author Comment

by:GPSPOW
ID: 40567270
Unfortunately there is not a Calendar Table in the Sql Server.

I have always found that since the field data is formatted as datetime, the BETWEEN clause does not give me data after midnight on the end date.

Any suggestions?

Thanks

Glen
0
 
LVL 69

Expert Comment

by:Scott Pletcher
ID: 40567295
Yeah, never use BETWEEN with date/datetime.

Don't use a calendar table unless you absolutely require it.  Wasted I/O mostly, but also some logic errors.


(dbo.OeOrders.OrderDateTime >= CASE WHEN month(getdate()) = 1
     THEN CAST(year(getdate())-1 as varchar(4))+'0101'
     ELSE dateadd(year, datediff(year, 0, getdate()), 0) END    
  AND      
  dbo.OeOrders.OrderDateTime < CASE WHEN month(getdate()) = 1
     THEN CAST(year(getdate()) as varchar(4))+'0101'
     ELSE dateadd(month, datediff(month, 0, getdate()), 0) END
0
U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

 
LVL 65

Expert Comment

by:Jim Horn
ID: 40567303
I've used Calendar tables with many clients with success, especially those that have custom business days that they need to track, or a fiscal calendar that doesn't have the same boundaries as a regular calendar.
0
 
LVL 69

Expert Comment

by:Scott Pletcher
ID: 40567333
I've seen people use calendar tables to count the number of days in a month -- huge waste of resources.  Sometimes they get over-used for every calendar action, when they are actually only needed for a very small number of them.

I have a NonWorkDays table, not a full calendar table.  They're shorter, and by its very nature can't be used when it's not needed :-).
0
 
LVL 48

Accepted Solution

by:
PortletPaul earned 500 total points
ID: 40568617
Rather than a case expression containing an implied conversion of varchar to date, use dateadd() throughout:

WHERE (
      dbo.OeOrders.OrderDateTime >=
                  CASE WHEN MONTH(GETDATE()) = 1
                        THEN DATEADD(YEAR, DATEDIFF(YEAR, 0, GETDATE()) - 1, 0) -- Jan 1 last year
                        ELSE DATEADD(YEAR, DATEDIFF(YEAR, 0, GETDATE()), 0)
                  END
      AND dbo.OeOrders.OrderDateTime <
                  CASE WHEN MONTH(GETDATE()) = 1
                        THEN DATEADD(YEAR, DATEDIFF(YEAR, 0, GETDATE()), 0) -- Jan 1 this year
                        ELSE DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()), 0)
                  END
      )

Open in new window


test it with this:
declare @dt as datetime
set @dt = '20150117'

select
  CASE WHEN month(@dt) = 1 
     THEN dateadd(year, datediff(year, 0, @dt)-1, 0) -- Jan 1 last year
     ELSE dateadd(year, datediff(year, 0, @dt), 0) END     
  ,      
  CASE WHEN month(@dt) = 1 
     THEN dateadd(year, datediff(year, 0, @dt), 0) -- Jan 1 this year
     ELSE dateadd(month, datediff(month, 0, @dt), 0) END
;

declare @dt as datetime
set @dt = '20150324'

select
  CASE WHEN month(@dt) = 1 
     THEN dateadd(year, datediff(year, 0, @dt)-1, 0) -- Jan 1 last year
     ELSE dateadd(year, datediff(year, 0, @dt), 0) END     
  ,      
  CASE WHEN month(@dt) = 1 
     THEN dateadd(year, datediff(year, 0, @dt), 0) -- Jan 1 this year
     ELSE dateadd(month, datediff(month, 0, @dt), 0) END
;

Open in new window

0
 

Author Closing Comment

by:GPSPOW
ID: 40582642
Thanks

Worked very well.

Glen
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

825 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question