Add carriage return to a full address field

  • Hi all,

    I'm pulling individual address fields into my script, some of which are null.

    I would like to add in a carriage return after ONLY those fields that are NOT NULL.

    Any help will be appreciated.

    Current script is:

    SELECT

    ISNULL(SAD.HOUSENUMBER,'') + ISNULL(SAD.ADDRESS1,'') + ISNULL(SAD.ADDRESS2,'') + ISNULL(SAD.ADDRESS3,'') + ISNULL(SAD.CITY,'') + ISNULL(SAD.COUNTRY,'') + ISNULL(SAD.ZIP,'') as 'FULL_ADDRESS'

    FROM

    SFAADDRESS SAD

    Thanks and Regards

  • Concat NULL yelds NULL:

    SELECT

    ISNULL(SAD.HOUSENUMBER + CHAR(13) + CHAR(10),'') +

    ISNULL(SAD.ADDRESS1 + CHAR(13) + CHAR(10),'') +

    ISNULL(SAD.ADDRESS2 + CHAR(13) + CHAR(10),'') +

    ISNULL(SAD.ADDRESS3 + CHAR(13) + CHAR(10),'') +

    ISNULL(SAD.CITY + CHAR(13) + CHAR(10),'') +

    ISNULL(SAD.COUNTRY + CHAR(13) + CHAR(10),'') +

    ISNULL(SAD.ZIP + CHAR(13) + CHAR(10),'') as 'FULL_ADDRESS'

    FROM

    SFAADDRESS SAD

    -- Gianluca Sartori

  • You are a star 🙂

    Thank you very much

  • spaghettidba (4/16/2015)


    Concat NULL yelds NULL:

    SELECT

    ISNULL(SAD.HOUSENUMBER + CHAR(13) + CHAR(10),'') +

    ISNULL(SAD.ADDRESS1 + CHAR(13) + CHAR(10),'') +

    ISNULL(SAD.ADDRESS2 + CHAR(13) + CHAR(10),'') +

    ISNULL(SAD.ADDRESS3 + CHAR(13) + CHAR(10),'') +

    ISNULL(SAD.CITY + CHAR(13) + CHAR(10),'') +

    ISNULL(SAD.COUNTRY + CHAR(13) + CHAR(10),'') +

    ISNULL(SAD.ZIP + CHAR(13) + CHAR(10),'') as 'FULL_ADDRESS'

    FROM

    SFAADDRESS SAD

    I am curious, doesn't one CHAR(13) OR CHAR(10) work? Why both CHAR(13) and CHAR(10)? I have seen other people do this but have never understood why.

    "I cant stress enough the importance of switching from a sequential files mindset to set-based thinking. After you make the switch, you can spend your time tuning and optimizing your queries instead of maintaining lengthy, poor-performing code."

    -- Itzik Ben-Gan 2001

  • In windows the line separator is CR+LF. In linux it's LF.

    CR = CHAR(13)

    LF = CHAR(10)

    -- Gianluca Sartori

  • spaghettidba (4/16/2015)


    In windows the line separator is CR+LF. In linux it's LF.

    CR = CHAR(13)

    LF = CHAR(10)

    Got it. Thanks!

    "I cant stress enough the importance of switching from a sequential files mindset to set-based thinking. After you make the switch, you can spend your time tuning and optimizing your queries instead of maintaining lengthy, poor-performing code."

    -- Itzik Ben-Gan 2001

Viewing 6 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic. Login to reply