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,
SherryDeveloperAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:
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.
SherryDeveloperAuthor Commented:
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.
Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:
This would be the first part of the WHERE condition:
(   @OCCURED = 'After' and B.BHVOBS_TS >= @DATE_OCCURED
OR  @OCCURED = 'Before' and B.BHVOBS_TS <= @DATE_OCCURED
OR  @OCCURED = 'On' amd B.BHVOBS_TS = @DATE_OCCURED
OR  @OCCURED = 'Between' and B.BHVOBS_TS BETWEEN @DATE_OCCURED AND @CREATED_TO
/*   ELSE null    --@OCCURED = 'On' AND B.BHVOBS_TS = @DATE_OCCURED AND @OCCURED_TO IS NULL */
) 
AND
(
/* same with @CREATED ... */
)

Open in new window

I don't know what to do with the part I commented, because I can not get any sense out of it.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
10 Tips to Protect Your Business from Ransomware

Did you know that ransomware is the most widespread, destructive malware in the world today? It accounts for 39% of all security breaches, with ransomware gangsters projected to make $11.5B in profits from online extortion by 2019.

SherryDeveloperAuthor Commented:
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
Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:
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).
SherryDeveloperAuthor Commented:
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.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Query Syntax

From novice to tech pro — start learning today.