SQL query if select criteria is null output to be text

611 views Asked by At

I'm trying to add to an existing SQL query to output "No Data Found" if the entire query is null.

I want to display "No data Found" instead of showing a blank output.

Database I'm writing the SQL query is towards InterSystems Cache. Any help is greatly appreciated.

This is the query I'm working with

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',
sn.dict_2 AS 'Continue'
FROM db.sn
Where sn.dict_2 = 123

If the query isnull based on the where clause then output should show a text "NO Data Found for 123 today" instead of what is displaying a blank screen on my system. IF not isnull then will display the data.

IF Query isnull output should show
NO DATA FOUND FOR 123 TODAY

If Query not isnull output should show 
Attendance:                                 Continue:
Attended today. Also Acted with respect     Great
Left because not feeling well               Excussed
Went Home                                   Not Excused

Thanks

1

There are 1 answers

7
Tony On

Surround the CASE with an IsNull:

SELECT 
   IsNull(
      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 , 'No Data Found' )  AS 'Attendance'
FROM db.sn