2013-09-22 64 views

回答

2

下面的查詢並不做任何事情放在桌子上,但而不是顯示值,它顯示等效的字符串,

SELECT yourValue, 
     CASE WHEN yourValue > 55 THEN 'Normal' ELSE 'Abnormal' END 
FROM tableName 

56和起來會Normal
55和向下將是Abnormal

0

如果列允許空值,那麼你可以嘗試:

SELECT t.ColA, CASE WHEN t.ColA >= 55 THEN 'Normal value' WHEN t.ColA < 55 THEN 'abnormal value' END 
FROM MySchema.MyTable AS t 

例子:

DECLARE @MyTable TABLE 
(
    ID INT IDENTITY(1,1) PRIMARY KEY, 
    ColA INT NULL 
); 

INSERT INTO @MyTable (ColA) VALUES(11); 
INSERT INTO @MyTable (ColA) VALUES(111); 
INSERT INTO @MyTable (ColA) VALUES(NULL); 

SELECT t.ColA, 
CASE 
    WHEN t.ColA >= 55 THEN 'Normal value' 
    WHEN t.ColA < 55 THEN 'abnormal value' 
    -- WHEN t.ColA IS NULL THEN NULL 
END AS CaseWhen 
FROM @MyTable AS t 

結果:

ColA  CaseWhen 
----------- -------------- 
11   abnormal value 
111   Normal value 
NULL  NULL