Case statement insde where clause

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ddtpmyra
    Contributor
    • Jun 2008
    • 333

    #1

    Case statement insde where clause

    I'm trying to add my case statment inside the condition but I think I'm not doing this right. I want to run a list of orders based on current date.

    Let say if current date is monday I want to run the added orders made from friday 3pm.

    And if current date is tuesday then I want to run the orders added yesterday from 3pm.

    Having this condition all I have to do is to play with the getdate and dateadd. Now I'm having problems in my case condition and it looks like this.

    Code:
    select * from orders
    where 
    CASE WHEN GETDATE()=DATEADD(wk,DATEDIFF(wk,0,GETDATE()),0) --to get Monday
    THEN CONVERT(VARCHAR(10), DATEADD(D-3,GETDATE()), 121)+' 14:59:059.059' AND CONVERT(VARCHAR(10), GETDATE(), 121)+' 15:00:000.000'
    CASE WHEN  GETDATE()<>DATEADD(wk,DATEDIFF(wk,0,GETDATE()),0)
    THEN CONVERT(VARCHAR(10), DATEADD(D,-1,GETDATE()), 121)+' 14:59:059.059' AND CONVERT(VARCHAR(10), GETDATE(), 121)+' 15:00:000.000'
    END
    =ORDER_CREATETI MESTAMP
  • Rabbit
    Recognized Expert MVP
    • Jan 2007
    • 12517

    #2
    It should be along the lines of this
    Code:
    CASE DATEPART(WEEKDAY, GETDATE())
    WHEN 2 THEN DATEADD(D, -3, GETDATE())
    ELSE DATEADD(D, -1, GETDATE()) END

    Comment

    Working...