String Concatenation in SQL Server

When using '+' to concatenate two string field in SQL server (e.g. a+b), if one of them is NULL, then the whole string will become NULL. There is one solution to solve it:

COALESCE(a,'') + COALESCE(b,'')

So if any of the field is NULL, it will evaluate as an empty string. That's it!

Comments

  1. You could also use the more intuitive:
    ISNULL(a, '') + ISNULL(b, '')

    My Blog: http://theplague.wordpress.com/

    ReplyDelete
  2. Sir, you are a genius, and saved me from panicking all over the coming weekend.

    Thank you!!

    ReplyDelete

Post a Comment

Popular posts from this blog