我是SQL Server的新手,我想验证select语句中的列值是NULL还是空。
select语句为:
SELECT COM_TYPE, COM_IN_CHARGE_POSITION
FROM TABLE_01
如果COM_TYPE
的值为空或为空,我想以与COM_TYPE_OTHERS
相同的方式将COM_TYPE
的值设为COM_IN_CHARGE_POSITION
。
这仅仅是COM_TYPE和COM_IN_CHARGE_POSITION为null还是空,我想从其他两列中获取值。
有人可以帮我解决这个问题吗?
答案 0 :(得分:3)
让我假设空白为NULL
。在这种情况下,您只需要coalesce()
:
select coalesce(COM_TYPE, COM_TYPE_OTHERS, COM_IN_CHARGE_POSITION, COM_IN_CHARGE_POSITION_OTHERS) as effective_COM_TYPE
coalesce()
函数采用第一个非NULL
参数。如果值可以是空字符串或除NULL
之外还带有空格的字符串,则建议使用case
表达式。
答案 1 :(得分:2)
COALESCE
函数可以检查哪一个列第一个非NULL,但是如果要检查是null
还是空,则可以尝试使用CASE WHEN
SELECT CASE WHEN COM_TYPE IS NULL OR COM_TYPE = '' THEN COM_TYPE_OTHERS
ELSE COM_TYPE END AS COM_TYPE,
CASE WHEN COM_IN_CHARGE_POSITION IS NULL OR COM_IN_CHARGE_POSITION = '' THEN COM_IN_CHARGE_POSITION_OTHERS
ELSE COM_IN_CHARGE_POSITION END AS COM_IN_CHARGE_POSITION,
FROM TABLE_01
答案 2 :(得分:1)
您可以使用COALESCE()
或ISNULL()
或CASE WHEN
语句
SELECT COALESCE( COM_TYPE, COM_TYPE_OTHERS ) AS COM_TYPE,
COALESCE (COM_IN_CHARGE_POSITION , COM_IN_CHARGE_POSITION_OTHERS) AS COM_IN_CHARGE_POSITION
FROM TABLE_01
答案 3 :(得分:1)