Combine two column with sql statement

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • semomaniz
    Recognized Expert New Member
    • Oct 2007
    • 210

    Combine two column with sql statement

    I was able to combine three columns with following sql statement but the problem that i am having is if one of the column has a null value the concatenation does not work and the sql statement does not return any thing. I am not sure why this is happening. Is there a way to accomplish this even though the fields contain null value.

    Sql statement:

    Code:
    Select CAST(Customer.Contactid as nvarchar(10)) +'-'+ Customer.Firstname +','+ Customer.LastName as Name from Customer where customerid = 1547854
    Data types:

    Contactid -- int
    FirstName -- nvarchar
    LastName -- nvarchar

    Note: The above sql statement works perfect if non of the fields have null value.

    Thank you in advance
  • ck9663
    Recognized Expert Specialist
    • Jun 2007
    • 2878

    #2
    Yes.

    Use ISNULL() function.

    Try this:

    Code:
    Select isnull(CAST(Customer.Contactid as nvarchar(10)),'') +'-'+ isnull(Customer.Firstname,'') +','+ isnull(Customer.LastName,'') as Name from Customer where customerid = 1547854
    -- CK

    Comment

    • semomaniz
      Recognized Expert New Member
      • Oct 2007
      • 210

      #3
      It worked

      Thank you

      Comment

      Working...