Solved

If then in where clause

Posted on 2015-01-23
7
103 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
  • +1
7 Comments
 
LVL 66

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
The Ultimate Checklist to Optimize Your Website

Websites are getting bigger and complicated by the day. Video, images, custom fonts are all great for showcasing your product/service. But the price to pay in terms of reduced page load times and ultimately, decreased sales, can lead to some difficult decisions about what to cut.

 
LVL 66

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 49

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

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

695 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