开发者

T SQL Conditional String Concatenation

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 I should skip it and not enter any space.

select 
        case 
            when street_number != '' THEN (cast(street_number as int)) 
        end as street_number,
        case
            when street_ext != '' then
                    case
                        when street_ext = 50 then '1/2'
                    end
        end as street_ext,
        case
            when street_dir开发者_StackOverflow社区ect ! = '' then street_direct
        end as street_direct,
        case
            when site_street ! = '' then site_street
        end as site_street,
        case
            when site_address ! = '' then site_address
        end as site_address
    from parcel 

what I'd like to do is have a variable and assign it to the value of the first column street_number, then when I move on to the next column, street_ext, if it isn't null I'd like to check to see if the variable is null and if not, append a space and the value...and so on down the road.

I'm rusty as hell and could use a push in the right direction.

Thanks everyone.


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.


Nested isnulls could do what you need. Something like:

SELECT
     ISNULL(streetnumber + ' ', '')
       + ISNULL(streetext + ' ', '')
       etc

relying on the fact that NULL + ' ' = NULL.


Something along the lines of:

select coalesce(street_number+' ','')+
       coalesce(case when street_ext=50 then '1/2' else null end+' ','')+
       coalesce(street_direct+' ','')+
       coalesce(site_street+' ','')+
       coalesce(site_address,'')
from parcel


I have assumed your data types are all varchar or similar for simplicity. If you are OK with removing any double spaces, how about:

rtrim(ltrim(replace(isnull(street_number) + ' ' 
    + isnull(street_ext) + ' ' 
    + isnull(street_direct) + ' ' 
    + isnull(site_street) + ' ' 
    + isnull(site_address), '  ', ' ')))


First I would declare the seperator as a variable, because customers are notorious for changing these.

I would do this as follows:

DECLARE @AddressSeperator NVARCHAR(5) = ' '

...and then for the column declation, I'd use the following:

, CONCAT
(
    (CASE WHEN LEN(p.street_number) > 0 THEN p.street_number + @AddressSeperator ELSE '' END)
    , (CASE WHEN p.street_ext = 50 THEN '1/2' + @AddressSeperator WHEN LEN(p.street_ext) > 0 THEN p.street_ext + @AddressSeperator ELSE '' END)
    , (CASE WHEN LEN(p.street_direct) > 0 THEN p.street_direct + @AddressSeperator ELSE '' END)
    , (CASE WHEN LEN(p.site_street) > 0 THEN p.site_street + @AddressSeperator ELSE '' END)
    , ISNULL(p.site_address, '')

) AS [full_address]
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜