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,
Microsoft SQL Server 2008SSRSSQL

Avatar of undefined
Last Comment
Sherry
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
Sherry
Flag of United States of America image

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

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of Sherry
Sherry
Flag of United States of America image

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
Avatar of Qlemo
Qlemo
Flag of Germany image

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
Sherry
Flag of United States of America image

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.
SQL
SQL

SQL (Structured Query Language) is designed to be used in conjunction with relational database products as of a means of working with sets of data. SQL consists of data definition, data manipulation, and procedural elements. Its scope includes data insert, query, update and delete, schema creation and modification, and data access control.

61K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo