| Author |
Topic |
|
ceema
Yak Posting Veteran
80 Posts |
Posted - 2007-01-17 : 02:17:19
|
| I have a table SIM_TempCustomer where I have 5 fileds nad Values LikeSIM_TempCustomer----------------Val1.........Val2.........Val3.........Val4.........Val5---------------------------------------------------------A.............NUll........1.............Null.........3B.............Null........Null..........Null.........2C.............Null........6.............12...........NullWhat I want i, For the first row,Val2 is null,second row and third row also having val2 as null, so I have to leave it like that only.But first & third rows of Val3 are having values, Second row is not having any value, but since first & third row are having values, I have to get val3 of second row as '0'For Val4m third row is having value, so I have to replace val4's second and first rows with 0etc etc...Like the select statement of this SIM_TempCustomer should give a result likeVal1.........Val2.........Val3.........Val4.........Val5---------------------------------------------------------A.............NUll........1.............0............3B.............Null........0.............0............2C.............Null........6.............12...........0Could any one help me.ThanksCeema |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-01-17 : 02:27:39
|
| SELECT Val1, Val2, ISNULL(Val3, 0) AS Val3, ISNULL(Val4, 0) AS Val4, ISNULL(Val5, 0) AS Val5FROM SIM_TempCustomerPeter LarssonHelsingborg, Sweden |
 |
|
|
ceema
Yak Posting Veteran
80 Posts |
Posted - 2007-01-17 : 02:33:42
|
| Hello Peso, Here the problem is, Val2 is always not null...it can have values some times, so if I will replace it withISNULL(Val2, 0) AS Val2, then it will get replaced by 0. But it should not happen. If all the rows of val2 is null,it should remain like that, if val2 is having value, it should replace null only with '0'. I hope you understood my problem.Thank youCeema |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-01-17 : 02:35:46
|
Or more general-- prepare sample datadeclare @t table (val1 varchar(2), val2 int, val3 int, val4 int, val5 int)insert @tselect 'a', null, 1, null, 3 union allselect 'b', null, null, null, 2 union allselect 'c', null, 6, 12, null-- show the resultselect t.val1, isnull(t.val2, x.val2) as val2, isnull(t.val3, x.val3) as val3, isnull(t.val4, x.val4) as val4, isnull(t.val5, x.val5) as val5from @t as tcross join ( select case when max(val2) is null then null else 0 end as val2, case when max(val3) is null then null else 0 end as val3, case when max(val4) is null then null else 0 end as val4, case when max(val5) is null then null else 0 end as val5 from @t ) as x Peter LarssonHelsingborg, Sweden |
 |
|
|
ceema
Yak Posting Veteran
80 Posts |
Posted - 2007-01-17 : 02:39:05
|
| Hello Peso,Thank you so much, this will do.Thanks againCeema |
 |
|
|
|
|
|