Friday, May 25, 2007

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!

2 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

CSP on Apache

To add CSP to root if sort of funny. The following will NOT work for most cases !!     <LocationMatch "^/$">        Header s...