Monday, November 05, 2007

T-SQL: Concatenate something + null=null

Today I have lost 2 hours until I have discovered this rule. I wanted to concatenate the contents of a field in the database with a variable using something like that:

UPDATE TableName

SET FieldName=FieldName + ',' + @value

If the value of FieldName is null this operation is very stupid because the result will be null too. In my case I have forced that the field was not nullable (default value: '') and all runs.