user664481 user664481 - 5 months ago 17
SQL Question

SQL Server : replace null value

In SQL Server, how to write a query in case

ItemType = MD5
then replace
Null
value as 'NAM' ?

Country ItemType
Null MD5

Answer

Use CASE EXPRESSION :

SELECT CASE WHEN t.ItemType = 'MD5' THEN 'NAM' ELSE t.country END as [Country],
       t.itemType
FROM YourTable t

If you want it only to be replace when Country is NULL, then replace it with this:

CASE WHEN t.ItemType = 'MD5' AND t.country IS NULL THEN 'NAM' ELSE t.country END