| Author |
Topic |
|
cwtriguns2002
Constraint Violating Yak Guru
272 Posts |
Posted - 2007-03-07 : 00:32:54
|
| Hi. I want to make a query that concatenate the address of the person with a comma inbetween and neglecting the null value.table - pspersonaldatadcno name streetbldg1 streetbldg2 streetbldg31 jon santol1 null santol32 mike mangga1 mangga2 null3 jay langka1 langka2 langka34 joy null buko2 buko35 jean null null nullexpected output is:name addressjon santol1, santol3mike mangga1, mangga2jay langka1, langka2, langka3joy buko2, buko3jean nullthanks.-Ron- |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2007-03-07 : 00:44:23
|
[code]select name, address = isnull(streetbldg1, '') + isnull(', ' + streetbldg2, '') + isnull(', ' + streetbldg3, '') from pspersonaldata[/code] KH |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-03-07 : 01:05:20
|
| Buko doesn't handle very well.select name,case when address = ', , ' then nullwhen address like ', , %' then substring(address, 5, 8000)when address like ', %' then substring(address, 3, 8000)else addressendfrom (select name, address = isnull(streetbldg1, '') + isnull(', ' + streetbldg2, '') + isnull(', ' + streetbldg3, '') from pspersonaldata) as xPeter LarssonHelsingborg, Sweden |
 |
|
|
cwtriguns2002
Constraint Violating Yak Guru
272 Posts |
Posted - 2007-03-07 : 01:09:20
|
| the result for this is:name addressjon santol1, santol3 - OKmike mangga1, mangga2 - OKjay langka1, langka2, langka3 - OKjoy ,buko2, buko3 - NOT OK. It has comma before jean null - OK |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-03-07 : 01:17:27
|
| select name,case when address = ', , ' then nullwhen address like ', , %' then substring(address, 5, 8000)when address like ', %' then substring(address, 3, 8000)else replace(address, ', , ', ', ')endfrom (select name,address = isnull(streetbldg1, '') + isnull(', ' + streetbldg2, '') + isnull(', ' + streetbldg3, '') from pspersonaldata) as xPeter LarssonHelsingborg, Sweden |
 |
|
|
cwtriguns2002
Constraint Violating Yak Guru
272 Posts |
Posted - 2007-03-07 : 01:27:28
|
| Thank peso, KH.Your query works very well. But, it will not work if the field has no value at all. The field has no NULL value specified. Just an empty field.Thanks.-Ron- |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2007-03-07 : 01:31:43
|
empty field as in '' ?use nullif(), examplenullif(streetbldg1, '') KH |
 |
|
|
cwtriguns2002
Constraint Violating Yak Guru
272 Posts |
Posted - 2007-03-07 : 01:35:33
|
| yes, as in ''.use nullif(streetbldg1, '') where can i insert this in my query?thanks-Ron- |
 |
|
|
cwtriguns2002
Constraint Violating Yak Guru
272 Posts |
Posted - 2007-03-07 : 01:39:47
|
| i changed isnull to nullif... takes no effect. |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2007-03-07 : 01:51:07
|
no no no. the isnull still there. add a nullif to check for empty stringaddress = isnull(nullif(streetbldg1, ''), '') + isnull(', ' + nullif(streetbldg2, ''), '') + isnull(', ' + nullif(streetbldg3, ''), '') KH |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2007-03-07 : 02:04:59
|
extracted from BOLquote: ISNULLReplaces NULL with the specified replacement value.SyntaxISNULL ( check_expression , replacement_value )
quote: NULLIFReturns a null value if the two specified expressions are equivalent.SyntaxNULLIF ( expression , expression )
KH |
 |
|
|
cwtriguns2002
Constraint Violating Yak Guru
272 Posts |
Posted - 2007-03-07 : 02:18:38
|
| Thank u Thank u Thank u.Fantastic.1 more thing. The column name of the address is (No column Name). How could i change that?-Thanks--Ron- |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2007-03-07 : 02:19:58
|
use alias select name,case when address = ', , ' then nullwhen address like ', , %' then substring(address, 5, 8000)when address like ', %' then substring(address, 3, 8000)else replace(address, ', , ', ', ')end as address... KH |
 |
|
|
cwtriguns2002
Constraint Violating Yak Guru
272 Posts |
Posted - 2007-03-08 : 21:24:04
|
| Hi all. How can I concatenate integer variables? I think i have to convert them first to string, but dont know how to do it.select [SY] = year1 + '-' + year2 from hrappempeducsit returns : 4005my expected result is: 2000 - 2005Thanks.-Ron- |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2007-03-08 : 21:25:26
|
convert to string. use cast() or convert()select [SY] = convert(varchar(10), year1) + '-' + convert(varchar(10), year2) from hrappempeducsselect [SY] = cast(year1 as varchar(10)) + '-' + cast(year2 as varchar(10)) from hrappempeducs KH |
 |
|
|
|
|
|