SQL case with conditions

I have a query set that contains a CASE statement that works 95% of the time ... That's another 5% due to missing data. There is other data pointing to pointers to help ... I just know if this is possible ...

So: CASE PRDE.STATUSCODE WHEN "ONLY" RETURNS "WHEN" "DAY" DELETE "WHEN" P "THEN" Pending "WHEN" THEN "Satisfied" WHEN "T" THEN "IS SET" END AS STATUS

Sometimes this field is '', but the SATISFIEDDATE text field may be filled ... Can I write something here: CASE '' THEN [if SATISFIEDDATE <> '' then 'Satisfied']

Sorry if this is just silly ... :)

+1


a source to share


5 answers


CASE
    WHEN PRDE.STATUSCODE='A' THEN 'Appealed'
    WHEN PRDE.STATUSCODE='D' THEN 'Dismissed'
    WHEN PRDE.STATUSCODE='P' THEN 'Pending'
    WHEN PRDE.STATUSCODE='S' OR (PRDE.STATUSCODE='' AND LEN(SatisfiedDate)>0) THEN 'Satisfied'
    WHEN PRDE.STATUSCODE='T' THEN 'Settled'
    ELSE '?null/unknown?'
END AS STATUS

      



+2


a source


You may have a different CASE statement:

CASE '' THEN CASE WHEN SatisfiedDate != '' THEN 'Satisfied' END

      



An alternative might be the following:

CASE
    WHEN PRDE.STATUSCODE = 'A' THEN 'Appealed'
    WHEN PRDE.STATUSCODE = 'D' THEN 'Dismissed'
    WHEN PRDE.STATUSCODE = 'P' THEN 'Pending'
    WHEN PRDE.STATUSCODE = 'S'
       OR (PRDE.STATUSCODE = '' AND SatisfiedDate != '') THEN 'Satisfied'
    WHEN PRDE.STATUSCODE = 'T' THEN 'Settled'
END AS STATUS

      

+1


a source


You can use ELSE

ELSE else_result_expression

      

In else_result_expression, you can specify any legal expression you want, including what you wrote.

http://www.databasejournal.com/features/mssql/article.php/3288921/T-SQL-Programming-Part-5---Using-the-CASE-Function.htm

0


a source


you can do the following case arguments, either in your else clause or in your when clause

CASE PRDE.STATUSCODE 
    WHEN 'A' THEN 'Appealed' 
    WHEN 'D' THEN 'Dismissed' 
    WHEN 'P' THEN 'Pending' 
    WHEN 'S' THEN 'Satisfied' 
    WHEN 'T' THEN 'Settled' 
    else
        case 
            when SATISFIEDDATE is not null then 'Satisfied'
        end
END AS STATUS

      

0


a source


Perhaps this is what you are looking for:

CASE PRDE.STATUSCODE 
WHEN 'A' THEN 'Appealed' 
WHEN 'D' THEN 'Dismissed' 
WHEN 'P' THEN 'Pending' 
WHEN 'S' THEN 'Satisfied' 
WHEN 'T' THEN 'Settled' 
WHEN '' AND NULLIF(PRDE.SATISFIEDDATE, '') IS NOT NULL THEN 'Satisfied' END AS STATUS

      

0


a source







All Articles