CASE语句未获取NULL值

时间:2018-07-05 23:33:57

标签: sql tsql

我写了以下CASE语句

CASE PaymentType
  WHEN NULL THEN 'Check'
  WHEN 512 THEN 'Check'
  WHEN 0 THEN 'Check'
  WHEN 32 THEN 'ACH'
  WHEN 544 THEN 'ACH'
  ELSE 'Card'
  END AS 'Current Acceptance',

NULL值虽然属于ELSE类别(即卡),而不是按预期的“检查”。

更改此值以使NULL值返回“ check”会很简单吗?

2 个答案:

答案 0 :(得分:2)

不起作用。 NULLNULL表达式中甚至与case不匹配。

使用显式比较:

(CASE WHEN PaymentType IS NULL OR
           PaymentType IN (512, 0)
      THEN 'Check'
      WHEN PaymentTYpe IN (32, 544)
      THEN 'ACH'
      ELSE 'Card'
 END) AS Current_Acceptance,

答案 1 :(得分:1)

在这种情况下,我认为TSQL对于“ = NULL”和“ IS NULL”比较挑剔。

尝试一下:

CASE 
  WHEN PaymentType IS NULL THEN 'Check'
  WHEN PaymentType = 512 THEN 'Check'
  WHEN PaymentType = 0 THEN 'Check'
  WHEN PaymentType = 32 THEN 'ACH'
  WHEN PaymentType = 544 THEN 'ACH'
  ELSE 'Card'
 END AS 'Current Acceptance'