Solved

If then in where clause

Posted on 2015-01-23
7
98 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:ScottPletcher
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
Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

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

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.

Question has a verified solution.

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

Suggested Solutions

Let's review the features of new SQL Server 2012 (Denali CTP3). It listed as below: PERCENT_RANK(): PERCENT_RANK() function will returns the percentage value of rank of the values among its group. PERCENT_RANK() function value always in be…
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

911 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now