2010-06-15 7 views
10

De acuerdo con la siguiente descripción, tengo que enmarcar una declaración CASE...END en SQL Server, me ayude a enmarcar una declaración compleja CASE...END para cumplir la siguiente condición.Combinación de varias condiciones en una declaración de caso único en el servidor Sql

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' 

Gracias de antemano.

Respuesta

32

Usted puede poner la condición después de la cláusula WHEN, así:

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

Por supuesto, el argumento podría ser que las reglas complejas de este tipo pertenecen en su capa de lógica de negocio, no en un procedimiento almacenado en la base de datos ...

+0

Muchísimas gracias :) Que tengas un buen día – 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" 
Cuestiones relacionadas