简体   繁体   中英

SQL CONCAT statement with multiple conditions

I have a table with output as of following:

样品

I am trying to concat the outputs into a single output, given the conditions that:

  • When column fields (Type 1 to Type 5) IS NOT NULL OR '' , take the value and combine it with another field with the same condition met

Expected output(based on screenshot above): RAM/TOTAL/NA

  • When column field (Type 6) IS NOT NULL OR '' , display the result as OUT OF SERVICE, ignoring other values

Expected output: OUT OF SERVICE

Sample query:

SELECT CONCAT(Type1, '/' , Type2, '/' , Type3, '/' , Type4, '/' , Type5, '/' , Type6) AS OUTPUT FROM #myTable

You example screen shots do not agree with your expected output.

You say if Type6 is Null or blank, then it should say 'OUT OF SERVICE'

However, in your screen shot, your expected output concats the previous columns though Type6 is blank! Should it not say OUT OF SERVICE ??

Assuming that this is simply a typo.. what you could do is the following:

Set the values of the column to null if blank and then concat them in the subsequent select. Just one one many ways to do it..

;with mycte as 
    (select
        '' as Type1
        , 'RAM' as Type2
        ,'' as Type3
        ,'TOTAL' as Type4
        ,'N/A' as Type5
        ,'' as Type6
    )

,set_null as (
select 

    Type1 =  case when isnull(Type1,'') = '' then NULL else Type1+'/' end
    ,Type2 =  case when isnull(Type2,'') = '' then NULL else Type2+'/' end
    ,Type3 =  case when isnull(Type3,'') = '' then NULL else Type3 +'/'end
    ,Type4 =  case when isnull(Type4,'') = '' then NULL else Type4+'/' end
    ,Type5 =  case when isnull(Type5,'') = '' then NULL else Type5+'/' end
    ,Type6 =  case when isnull(Type6,'') = '' then NULL else Type6 end

from mycte


)

 select 
     case
        when isnull(Type6,'') = '' then 'OUT OF SERVICE' 
        else concat(Type1+Type2,Type3,Type4,Type5,Type6) 
      end as concat_column

 from set_null

You may try this. This is an approach use to remove unnecessary character added due to no null blank value from a string.

First create your string and remove the duplicate unnecessary / from the string.
At the end only need to check whether / is added in suffix or prefix which need to be removed, by using simple case and substring you can achieve the same.


;with cte as 
    (select 'sd' as Type1, 'RAM' as Type2,'' as Type3,'TOTAL' as Type4,'N.A' as Type5,null as Type6)
,ct as (
    SELECT case when Type6 is null     ---- you may chnage your condition of checking Type6 is over there
        then 'OUT OF SERVICE' 
        else REPLACE( REPLACE( CONCAT('][', Type1, '][' , Type2, '][' , Type3, '][' , Type4, '][' , Type5, '][' , Type6), '[]', ''), '][', '/') 
        End AS OUTP FROM cte 
    )
    select  
    case when OUTP like '/%' 
        then case when OUTP like '%/' 
            then SUBSTRING( OUTP, 2, len(OUTP)-2 ) 
            else SUBSTRING( OUTP, 2, len(OUTP)-1 ) end 
        else case when OUTP like '%/' 
            then SUBSTRING( OUTP, 1, len(OUTP)-1 ) 
            else OUTP end 
        End as OUTPU 
    from ct


The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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