Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
Mondeo
Constraint Violating Yak Guru
287 Posts |
Posted - 2007-11-01 : 07:53:40
|
| I have thisSELECT vehicleref,manufacturer + ' ' + model + ' ' + derivative + ' ' + additionalfreetext AS Vehicle FROM tableThis works fine unless additionalfree text is NULL, then it returns NULL. Can I make it so that if any of the 4 fields are null then it just concatenates the remaining onesThanks |
|
|
elancaster
A very urgent SQL Yakette
1208 Posts |
Posted - 2007-11-01 : 07:58:03
|
| look at coalesce in BOLcoalesce(field,'')Em |
 |
|
|
Mondeo
Constraint Violating Yak Guru
287 Posts |
Posted - 2007-11-01 : 08:57:28
|
| Hi,Cant get this to work, if I useSELECT vehicleref,COALESCE(manufacturer,' ', model,' ',derivative,' ',additionalfreetext) AS Vehicle FROM tableIt just returns the manufacturer. |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2007-11-01 : 09:05:21
|
| You need to apply to all the columnsSELECT vehicleref,coalesce(manufacturer,'') + ' ' + coalesce(model,'') + ' ' + coalesce(derivative,'') + ' ' + coalesce(additionalfreetext,'') AS Vehicle FROM tableMadhivananFailing to plan is Planning to fail |
 |
|
|
Mondeo
Constraint Violating Yak Guru
287 Posts |
Posted - 2007-11-01 : 09:15:13
|
| Got it!Cheers |
 |
|
|
JBelthoff
Posting Yak Master
173 Posts |
Posted - 2007-11-01 : 15:51:17
|
Just curios, but wouldn't IsNull() work better here?Select IsNull(vehicleref, '') ,IsNull(manufacturer,"") + ' ' + IsNull(model, '') + ' ' + IsNull(derivative, '') + ' ' + IsNull(additionalfreetext, '') As Vehicle From table JBelthoff• Hosts Station is a Professional Asp Hosting Provider• Position SEO can provide your company with SEO Services at an affordable price› As far as myself... I do this for fun! |
 |
|
|
Kristen
Test
22859 Posts |
Posted - 2007-11-01 : 16:23:03
|
"Just curios, but wouldn't IsNull() work better here?"Same thing but:IsNull takes only 2 parameters, COALESCE takes unlimited number of parameterCOALESCE is standards-compliantIsNull has some unexpected side effects if datatypes of the parameters are differentand finallyIsNull is a rubbish name for something that returns the first NON-null parameter! Although I have to admit that COALESCE is not really a word in common-English-parlance Note thatcoalesce(manufacturer,'') + ' ' + coalesce(model,'') + ' ' is liable to give you multiple adjacent spaces if a parameter is missing, so you might want to do:coalesce(manufacturer + ' ', '') + coalesce(model + ' ','') + ... insteadKristen |
 |
|
|
|
|
|
|
|