SQL使用带有STRING功能的CASE

时间:2014-11-04 20:15:56

标签: sql string case intersystems-cache

我正在尝试编写一个SQL查询,它一起使用CASE和STRING并收到错误。 这就是我正在努力的方法。任何帮助是极大的赞赏。 我尝试添加STRING功能,但也不起作用。

SELECT Case
         when sn.1_code =  1 then 'Attended  -- ' ,  
                                  sn.mult_1 , 
                                  , 'and'  ,  
                                  sn.dict_2 ,  
                                  ' also acted with ' ,  
                                  sn.dict_3 , 
                                  '.' , 
         when sn.1_code =  3 then 'left because ' ,
                                  sn.mult_2 ,
                                  '.' ,
         when sn.dict_1 =  2 then 'Went home' ,
         when sn.dict_1 = 24 then 'Canceled' AS 'Attendance'
FROM db.sn

4 个答案:

答案 0 :(得分:4)

看起来你正在尝试连接字符串。实际操作员可能会因服务器软件而异,但想法是:

SELECT
Case 
  when sn.1_code  = 1 
    then  'Attended  -- ' + sn.mult_1 + 'and' + sn.dict_2 + ' also acted with ' + sn.dict_3 + '.' 
  when sn.1_code  = 3 
    then 'left because ' + sn.mult_2 + '.' , 
  when  sn.dict_1 = 2 
    then 'Went home'  ,
  when sn.dict_1 = 24 
    then 'Canceled'   
End AS 'Attendance'
FROM db.sn

答案 1 :(得分:2)

Intersystems Cache似乎支持两个参数concat()函数(ala Oracle)。它还支持string()。这应该做你想要的:

SELECT (Case when sn.1_code =  1
            then string('Attended  -- ', sn.mult_1, 'and',  sn.dict_2 ,  ' also acted with ', sn.dict_3 ,  '.' ) 
            when sn.1_code =  3
            then string('left because ', sn.mult_2 , '.' )
            when sn.dict_1 =  2
            then 'Went home'
            when sn.dict_1 = 24
            then 'Canceled'
        end) AS 'Attendance'
FROM db.sn;

case语句还需要end

答案 2 :(得分:1)

正如其他答案所指出的那样,您需要将字符串值连接在一起。从我对Intersystems Cache SQL的了解非常少(我只是查找它),你将need to use ||连接值(你也可以使用CONCAT()函数来做到这一点,但它只是允许两个参数:

SELECT Case
     when sn.1_code =  1 then 'Attended  -- ' || 
                              sn.mult_1 || 
                              'and'  ||  
                              sn.dict_2 ||  
                              ' also acted with ' ||  
                              sn.dict_3 || 
                              '.'  
     when sn.1_code =  3 then 'left because ' ||
                              sn.mult_2 ||
                              '.' 
     when sn.dict_1 =  2 then 'Went home' 
     when sn.dict_1 = 24 then 'Canceled' END AS 'Attendance'
FROM db.sn

您还有一些额外的逗号,以及CASE statement

末尾缺少的END

答案 3 :(得分:0)

如果你试图连接,就像在我看来,你用+字符而不是逗号来做:

SELECT
Case when sn.1_code  = 1 then  'Attended  -- ' +  
sn.mult_1 +
'and' +
sn.dict_2 +  
' also acted with ' +
sn.dict_3 +
'.' 
when sn.1_code  = 3 
then 'left because ' + sn.mult_2 + '.'  
when  sn.dict_1 = 2 then 'Went home'  
when sn.dict_1 = 24 then 'Canceled' 
END  AS 'Attendance'
FROM db.sn

如果没有看到你的String函数示例,我不知道你试图用它做什么。