SQL判断如果一列值为null则取另一列值代替 isnull()

SELECT TOP 1000 [chClientCode]
      ,[nvcClientName]
      ,[chRegionCode]
      ,isnull(chUltimateHeadClientCode,[chClientCode]) as chUltimateHeadClientCode
  FROM [PwCMDM_V1].[Core].[tblClient]

Define:

isnull(expression,replacement_value)

[*] 使用时候发现一个问题,就是如果expression的长度小于replacement_value 系统会自动截取replacement_value 得到的结果肯定不正确. 所以使用起来还是有局限性的.

最后还是使用 CASE ... WHEN ... THEN... 的方式来实现的. 

SELECT TOP 1000 [chClientCode]
      ,[nvcClientName]
      ,[chRegionCode]
      ,CASE isnull(cl.chUltimateHeadClientCode,'') WHEN '' THEN cl.chClientCode ELSE cl.chUltimateHeadClientCode END as UHCCode
  FROM [PwCMDM_V1].[Core].[tblClient]
原文地址:https://www.cnblogs.com/dangkei/p/4119052.html