T SQL Conditional String Concatenation

后端 未结 5 814
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-05 16:55

Have a 5 columns of address data. I need to concatenate these fields into a single address with spaces in between the values if they exist. If the column has a null value

5条回答
  •  隐瞒了意图╮
    2021-01-05 17:23

    Use the "+" to concatenate strings in TSQL:

    SELECT CASE 
             WHEN LEN(p.street_number) > 0 THEN p.street_number + ' ' 
             ELSE '' 
           END +
           CASE 
             WHEN p.street_ext = 50 THEN '1/2'
             WHEN LEN(p.street_ext) > 0 THEN ''
             ELSE p.street_ext
           END + ' ' +
           CASE 
             WHEN LEN(p.street_direct) > 0 THEN p.street_direct + ' '
             ELSE ''
           END + 
           CASE 
             WHEN LEN(p.site_street) > 0 THEN p.site_street + ' '
             ELSE ''
           END  + 
           CASE 
             WHEN LEN(p.site_address) > 0 THEN p.site_address + ' '
             ELSE ''
           END AS full_address
    FROM PARCEL p
    

    The LEN function returns zero if the string value is NULL, or a zero length string.

提交回复
热议问题