We help IT Professionals succeed at work.

Adding second check to case statement give error.

MikeM670
MikeM670 used Ask the Experts™
on
I know that some of this query would best be done with the application layer instead of in sql but I have to work this way.

The query contains code for a checkbox.  If the checkbox is selected and the criteria for the date range is valid it will return records based on them.  If the checkbox is not selected the case statement will fall to the next option,  

-- [PSIMSQQ From_Date(Format:mm/dd/yyyy)] 00:00:00
-- [PSIMSQQ To_Date(Format:mm/dd/yyyy)] 23:59:59
-- [PSIMSQQ Checkbox(Format: By Arrest Date)]


SELECT CASE
            WHEN '[+PSIMSQQ Checkbox(Format: By Arrest Date)]' = 'on' AND (cpc.arrestdate >= '[+PSIMSQQ From_Date(Format:mm/dd/yyyy)] 00:00:00' or  cpc.arrestdate <= '[+PSIMSQQ To_Date(Format:mm/dd/yyyy)] 23:59:59')
               THEN cpc.arrestdate
               ELSE cm.occurredstartdate
       END as Date, *
FROM casemaster cm, CasePersonCharges cpc

Open in new window


The above code shows no errors in manager but if I change the code to

-- [PSIMSQQ From_Date(Format:mm/dd/yyyy)] 00:00:00
-- [PSIMSQQ To_Date(Format:mm/dd/yyyy)] 23:59:59
-- [PSIMSQQ Checkbox(Format: By Arrest Date)]


SELECT CASE
            WHEN '[+PSIMSQQ Checkbox(Format: By Arrest Date)]' = 'on' AND (cpc.arrestdate >= '[+PSIMSQQ From_Date(Format:mm/dd/yyyy)] 00:00:00' or  cpc.arrestdate <= '[+PSIMSQQ To_Date(Format:mm/dd/yyyy)] 23:59:59')
               THEN cpc.arrestdate
               ELSE IF cm.occurredstartdate >= '[+PSIMSQQ From_Date(Format:mm/dd/yyyy)] 00:00:00' or  cm.occurredstartdate <= '[+PSIMSQQ To_Date(Format:mm/dd/yyyy)] 23:59:59'
			   THEN cm.occurredstartdate
			   ELSE ''
       END as Date, *
FROM casemaster cm, CasePersonCharges cpc

Open in new window


It now contains errors.

Just a brief explanation on some of the code.

-- [PSIMSQQ From_Date(Format:mm/dd/yyyy)] 00:00:00
-- [PSIMSQQ To_Date(Format:mm/dd/yyyy)] 23:59:59
-- [PSIMSQQ Checkbox(Format: By Arrest Date)]

Open in new window


is used by the application to force the display of the parameters in this order.
Where you see '[+PSIMSQQ Checkbox(Format: By Arrest Date)]' (the + in front of the PSIMSQQ) tells the application program to use the value already entered previously.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Senior DBA
Most Valuable Expert 2018
Top Expert 2014
Commented:
You can't have multiple ELSEs in a CASE, nor can you use an IF in a CASE.

Try this instead:

SELECT CASE
            WHEN '[+PSIMSQQ Checkbox(Format: By Arrest Date)]' = 'on' AND (cpc.arrestdate >= '[+PSIMSQQ From_Date(Format:mm/dd/yyyy)] 00:00:00' or  cpc.arrestdate <= '[+PSIMSQQ To_Date(Format:mm/dd/yyyy)] 23:59:59')
               THEN cpc.arrestdate
            WHEN cm.occurredstartdate >= '[+PSIMSQQ From_Date(Format:mm/dd/yyyy)] 00:00:00' or  cm.occurredstartdate <= '[+PSIMSQQ To_Date(Format:mm/dd/yyyy)] 23:59:59'
                     THEN cm.occurredstartdate
                  ELSE ''
       END as Date, *
FROM casemaster cm, CasePersonCharges cpc

Author

Commented:
Scott,

That did the trick. I can go on and modify this to add the extra information I need returned.  Funny how your example was obvious when I saw it.

Thanks

MIke
Scott PletcherSenior DBA
Most Valuable Expert 2018
Top Expert 2014

Commented:
You're welcome.  A lot of times a quick example is far better than a long-winded description of the solution.