2010-06-15 9 views
10

In base alla seguente descrizione, devo strutturare un'istruzione CASE...END nel server SQL, aiutarmi a creare una complessa istruzione CASE...END per soddisfare la seguente condizione.Combinazione di più condizioni nell'istruzione di un singolo caso in Sql Server

if PAT_ENT.SCR_DT is not null and PAT_ENTRY.ELIGIBILITY is null then display display 'Favor' 
if PAT_ENT.SCR_DT is not null and PAT_ENTRY.EL is equal to No, display 'Error' 
if PAT_ENTRY.EL is Yes and DS.DES is equal to null or OFF, display 'Active' 
if DS.DES is equal to N, display 'Early Term' 
if DS.DES is equal to Y, display 'Complete' 

Grazie in anticipo.

risposta

32

Si può mettere la condizione dopo la clausola WHEN, in questo modo:

SELECT 
    CASE 
    WHEN PAT_ENT.SCR_DT is not null and PAT_ENTRY.ELIGIBILITY is null THEN 'Favor' 
    WHEN PAT_ENT.SCR_DT is not null and PAT_ENTRY.EL = 'No' THEN 'Error' 
    WHEN PAT_ENTRY.EL = 'Yes' and ISNULL(DS.DES, 'OFF') = 'OFF' THEN 'Active' 
    WHEN DS.DES = 'N' THEN 'Early Term' 
    WHEN DS.DES = 'Y' THEN 'Complete' 
    END 
FROM 
    .... 

Naturalmente, l'argomento potrebbe essere fatto che regole complesse di questo tipo appartengono nel vostro livello di logica di business, non in una stored procedure in il database ...

+0

Un sacco di grazie :) Buona giornata – swetha

0
select ROUND(CASE 

WHEN CONVERT(float, REPLACE(isnull(value1,''),',',''))='' AND CONVERT(float, REPLACE(isnull(value2,''),',',''))='' then CONVERT(float, REPLACE(isnull(value3,''),',','')) 

WHEN CONVERT(float, REPLACE(isnull(value1,''),',',''))='' AND CONVERT(float, REPLACE(isnull(value2,''),',',''))!='' then CONVERT(float, REPLACE(isnull(value3,''),',','')) 

WHEN CONVERT(float, REPLACE(isnull(value1,''),',',''))!='' AND CONVERT(float, REPLACE(isnull(value2,''),',',''))='' then CONVERT(float, REPLACE(isnull(value3,''),',','')) 

else CONVERT(float, REPLACE(isnull(value1,''),',','')) end,0) from Tablename where ID="123" 
Problemi correlati