简体   繁体   English

在select查询中使用多个case语句

[英]Using multiple case statements in select query

Hi I have a following query that checks for code to determine when was it entered or viewed. 嗨,我有一个以下查询,检查代码以确定何时输入或查看。

        declare @timestamp datetime;
        select
          case @timestamp
          when a.updatedDate =1760 then 'Entered on' +a.updatedDate
          when a.updatedDate=1710 then  'Viewed on' +a.updatedDate
          else 'Last Updated on'+ a.updatedDate
          end 
         from t_mainTable a
         where a.id=@Id;

When i try to run this query it gives me error 当我尝试运行此查询时,它给了我错误

Msg 102, Level 15, State 1, Procedure p_xxxx, line 40
Incorrect syntax near '='.   

There is some syntex error in the when lines. when行中有一些syntex错误。 Please let me know how to correct this Thanks 请让我知道如何纠正这个谢谢

There are two ways to write case statements, you seem to be using a combination of the two 有两种方法可以编写case语句,你似乎正在使用两者的组合

case a.updatedDate
    when 1760 then 'Entered on' + a.updatedDate
    when 1710 then 'Viewed on' + a.updatedDate
    else 'Last Updated on' + a.updateDate
end

or 要么

case 
    when a.updatedDate = 1760 then 'Entered on' + a.updatedDate
    when a.updatedDate = 1710 then 'Viewed on' + a.updatedDate
    else 'Last Updated on' + a.updateDate
end

are equivalent. 是等价的。 They may not work because you may need to convert date types to varchars to append them to other varchars. 它们可能无法工作,因为您可能需要将日期类型转换为varchars以将它们附加到其他varchars。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM