在SQL Server中,如何编写查询,以防ItemType = MD5
,然后将Null
值替换为'nam'?
Country ItemType
Null MD5
使用 CASE EXPRESSION
:
SELECT CASE WHEN t.ItemType = 'MD5' THEN 'NAM' ELSE t.country END as [Country],
t.itemType
FROM YourTable t
如果您只想在Country is NULL
时替换它,请用此替换:
CASE WHEN t.ItemType = 'MD5' AND t.country IS NULL THEN 'NAM' ELSE t.country END