具有不同数据类型的MySQL CASE语句

时间:2018-01-12 11:08:00

标签: mysql

我有以下代码(在Oracle BI中)

SELECT CASE 
         WHEN 
Year("Assignment Inactive Date") = 4712 
       THEN 'THIS' 
         ELSE 'ELSE' 
       END 
FROM   thisTable 

这很有效。相应地显示THISELSE。但是,如果我将其更改为以下内容,则会收到错误错误:expected CHAR got DATE

SELECT CASE 
         WHEN 
Year("Assignment Inactive Date") = 4712 
       THEN 'THIS' 
         ELSE "Assignment Inactive Date" 
       END 
FROM   thisTable 

最后,如果我执行以下操作,则一切都是空白的。返回行,但一切都是空白的。有什么想法吗?

SELECT CASE 
         WHEN 
Year("Assignment Inactive Date") <> 4712 
       THEN "Assignment Inactive Date" 
       END 
FROM   thisTable 

显示A和B

SELECT CASE
   WHEN
      year("Assignment Inactive Date") = 4712 
   THEN
      'A' 
   ELSE
      'B'
END

仅显示A的

SELECT CASE
   WHEN
      year("Assignment Inactive Date") = 4712 
   THEN
      'A' 
   ELSE
      CAST("Assignment Inactive Date" AS VARCHAR(20)) 
END

0 个答案:

没有答案