Search code examples
sqlstringcaseintersystems-cache

SQL using CASE with STRING function


I'm trying to write a SQL query that uses CASE and STRING together and getting an error. This is what I'm trying to work on. Any help is greatly appreciated. I tried adding in STRING function as well but also does not work.

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

Solution

  • As other answers have pointed out, you need to concatenate your string values together. From the very very very little I know of Intersystems Cache SQL (I just looked it up), you will need to use || to concatenate the values (you can also use the CONCAT() function to do this, but it only allows two paramaters):

    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
    

    You also had some extra commas in there, as well as a missing END at the end of your CASE statement