Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Concatenating columns returns NULL results Expand / Collapse
Author
Message
Posted Sunday, November 10, 2013 12:15 PM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Thursday, May 1, 2014 2:22 PM
Points: 24, Visits: 94
When I concatenate 2 columns, it returns NULL result. Column1 has some NULL value. How do I ensure it displays Column2 text even though column1 has NULL value.

Below is a snapshot.


Column1 Column2
_______________________

Test 1234
NULL 5968

______________________________
Current Result (Column1+Column2)
______________________________
Test1234
NULL

_______________________________
Desired Result (Column1+Column2)
______________________________
Test1234
5968


Post #1512979
Posted Sunday, November 10, 2013 12:32 PM


SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Yesterday @ 3:53 PM
Points: 1,780, Visits: 5,750
You can use COALESCE or ISNULL for this.

COALESCE(Column1,'')+COALESCE(Column2,'')

OR

ISNULL(Column1,'') + ISNULL(Column2,'')

Both functions will replace a NULL value with an empty string and prevent your problem.


MM


  • MMGrid Addin
  • MMNose Addin


  • Forum Etiquette: How to post Reporting Services problems
  • Forum Etiquette: How to post data/code on a forum to get the best help - by Jeff Moden
  • How to Post Performance Problems - by Gail Shaw

  • Post #1512983
    Posted Monday, November 11, 2013 6:00 AM


    SSCrazy

    SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

    Group: General Forum Members
    Last Login: 2 days ago @ 5:01 AM
    Points: 2,840, Visits: 3,968
    Basic concept (with default settings)

    select null + abc => null

    anything added/concatenated to null is null.

    and see this too http://technet.microsoft.com/en-us/library/ms176056(v=sql.105).aspx



    -------Bhuvnesh----------
    I work only to learn Sql Server...though my company pays me for getting their stuff done
    Post #1513103
    Posted Monday, November 11, 2013 9:15 AM
    SSC Eights!

    SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!

    Group: General Forum Members
    Last Login: 2 days ago @ 11:59 AM
    Points: 876, Visits: 2,419
    See the SQLServerCentral article Best Practice : coalesce vs. isnull? before you make a choice on which one to use!
    Post #1513177
    Posted Tuesday, November 12, 2013 2:33 PM
    Grasshopper

    GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

    Group: General Forum Members
    Last Login: Thursday, May 1, 2014 2:22 PM
    Points: 24, Visits: 94
    Thanks. It worked.
    Post #1513637
    « Prev Topic | Next Topic »

    Add to briefcase

    Permissions Expand / Collapse