Link to home
Start Free TrialLog in
Avatar of Sherry
SherryFlag for United States of America

asked on

Using <, >, <=, >=, = in a case statement for sql.

I have a stored procedure I'm writing and I need, or think I need, to use two case statements.  They are below.  But I keep getting an error because of the > in the Then part of the statement.  Can someone show me what I'm doing wrong?

            --CASE WHEN @OCCURED = 'After' THEN B.BHVOBS_TS >= @DATE_OCCURED
            --       WHEN @OCCURED = 'Before' THEN B.BHVOBS_TS <= @DATE_OCCURED
            --       WHEN @OCCURED = 'On' THEN B.BHVOBS_TS = @DATE_OCCURED
            --       WHEN @OCCURED = 'Between' THEN B.BHVOBS_TS BETWEEN @DATE_OCCURED AND @CREATED_TO
            --      ELSE null    --@OCCURED = 'On' AND B.BHVOBS_TS = @DATE_OCCURED AND @OCCURED_TO IS NULL
            --END AS Date_Occured,
            

            
            --CASE WHEN @CREATED = 'After' THEN B.BHVOBS_TS >= @DATE_CREATED
            --       WHEN @CREATED = 'Before' THEN B.BHVOBS_TS <= @DATE_CREATED
            --       --WHEN @CREATED = 'On' THEN B.BHVOBS_TS = @DATE_CREATED
            --       WHEN @CREATED = 'Between' THEN B.BHVOBS_TS BETWEEN @DATE_CREATED AND @CREATED_TO
            --      ELSE @CREATED = 'On' AND B.BHVOBS_TS = @DATE_CREATED AND @CREATED_TO IS NULL
            --END AS Date_Created,
Avatar of Qlemo
Qlemo
Flag of Germany image

This does not look like a WHERE with dynamic conditions, so what should be the result? You've written two cases, with each one showing a result as a column expression. But you never can use a condition as an expression:
  select x>y as one
would be similar.
Avatar of Sherry

ASKER

Ok.  Yes there are two, basically the same thing, but I need one for the date occurred and one for the date created.

So, I tried this in the where clause and had trouble there too.  Obviously, I'm not using this right.

I have a drop down, they select After, Before, On, Between and then set the date they want.  The records returned then need to have the date created and or returned to meet the criteria.  So, I'm thinking, somehow, I need to have it filter on the date occurred - always and possibly by the date created.
ASKER CERTIFIED SOLUTION
Avatar of Qlemo
Qlemo
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Sherry

ASKER

Sorry.  There will be a date picker for the date occurred, but if they select Between in the drop down, then a second date picker will show so that they can select  "to and from" dates
That isn't what the ELSE part tells me. The BETWEEN is covered by the clause I provided (and using the other var used for the upper boundary).
Avatar of Sherry

ASKER

It works great.  Thank you so much for you help.  I think I just stared at it too long and couldn't get past using Case or something.

Thanks again.