1

我在下面创建了一个表。

CREATE TABLE TEST_NULL
(
ID INT,
NAME VARCHAR(10),
PLACE VARCHAR(10),
ADDRESS VARCHAR(10),
ERRORMESSAGE VARCHAR(100)
)

INSERT INTO TEST_NULL
VALUES
(1, 'ABC', 'BLR', 'WHT', NULL),
(2, 'DEF', 'BLR', NULL, NULL),
(3, 'GHI', NULL, NULL, NULL),
(4, 'JKL', NULL, 'WHT', NULL)

SELECT * FROM TEST_NULL

当我对表进行选择时,我希望 ERRORMESSAGE 列的预期结果如下。

我需要获得第一条记录:

1, 'ABC', 'BLR', 'WHT', NULL

我需要获得第二条记录:

2, 'DEF', 'BLR', NULL, 'Address is NULL'

我需要获得第三条记录:

3, 'GHI', NULL, NULL, 'Place, Address is NULL'

我需要获得第四条记录:

4, 'JKL', NULL, 'WHT', 'Place is NULL'

任何帮助表示赞赏。

谢谢。

4

2 回答 2

2

我认为使用CASE表达式生成错误消息是最干净的方法:

SELECT ID, NAME, PLACE, ADDRESS,
       CASE WHEN PLACE IS NULL AND ADDRESS IS NULL THEN 'Place, Address is NULL'
            WHEN PLACE IS NULL                     THEN 'Place is NULL'
            WHEN ADDRESS IS NULL                   THEN 'Address is NULL'
            ELSE NULL END
FROM TEST_NULL
于 2017-02-24T13:44:58.993 回答
0

使用Case条款:-

SELECT ID,NAME,PLACE,ADDRESS, case 

            when
                PLACE is not null and ADDRESS is not null 
            then null 
            when 
                PLACE is null and ADDRESS is not null 
            then 'PLACE is NULL'
            when 
                PLACE is not null and ADDRESS is null 
            then 'ADDRESS is NULL'
            when 
                PLACE is null and ADDRESS is  null 
            then 'PLACE, Address is NULL'
            end as ERRORMESSAGE  

FROM TEST_NULL

结果:-

ID  NAME    PLACE   ADDRESS ERRORMESSAGE
1   ABC     BLR      WHT        NULL
2   DEF     BLR     NULL    ADDRESS is NULL
3   GHI     NULL    NULL    PLACE, Address is NULL
4   JKL    NULL      WHT    PLACE is NULL
于 2017-02-24T13:50:30.450 回答