Friday, December 17, 2010

isnull is a Saviour

I was trying to query my userName from the database...it was stored as firstname, middlename and lastname and being the dunce I am, I did the simple

Select firstName+' '+MiddleName+' '+lastName as userName from members

Right ?

WRONG !!!

If any of the cols is a null, which usually is for the middle name, the concatanation returns null...

BOOOO

So, now am doing this instead

Select ltrim(isnull(FirstName,'')+' '+isnull(MiddleName, '')+' '+isnull(LastName,'')) as userName from members

and all is well in my world again !!!

No comments: