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 |
|
visa123
Yak Posting Veteran
54 Posts |
Posted - 2010-11-26 : 09:22:21
|
| In my table valuesShortState Division CA 1CA 2CA 3CA 4SELECT * FROM Table1 Sometimes the values will comeShortState Division CA 1CA 2Here i have some condition 1,2,3,4 values is there i have to give 1 if its no value in table give 0like CA,1,1,0,0Below is the query.but i cant put case condition in this.DECLARE @State VARCHAR(50)SET @State ='CA' Declare @OutputString varchar(max)SELECT @OutputString = IsNull(@OutputString + ',' + Division , Division)FROM( Select Distinct ShortState + ',' + Division as Division from Table1 where ShortState=@State) tbl PRINT @OutputString Can anyone help me for this?Visa.G |
|
|
nigelrivett
Master Smack Fu Yak Hacker
3385 Posts |
Posted - 2010-11-26 : 09:31:51
|
| SELECT @OutputString = coalesce(@OutputString + ',','') + case when Division is not null then '1' else '0' endfromor maybe you wantDECLARE @State VARCHAR(50)SET @State ='CA'Declare @OutputString varchar(max)SELECT @OutputString = coalesce(@OutputString + ',','') + case when Division is not null then '1' else '0' endFROM(select (Select i = 1 union all select 2 union all select 3 union all select 4) aleft joinTable1 ton t.division = a.iand ShortState=@State) tbl==========================================Cursors are useful if you don't know sql.SSIS can be used in a similar way.Beer is not cold and it isn't fizzy. |
 |
|
|
visa123
Yak Posting Veteran
54 Posts |
Posted - 2010-11-26 : 09:50:02
|
| Hithanks for your reply.I dont have any null for 3,4 division.bec there is no 3,4 th division row.My data will beCA 1CA 23,4 no record in my table. if use that DECLARE @State VARCHAR(50)SET @State ='CA'Declare @OutputString varchar(max)SELECT @OutputString = coalesce(@OutputString + ',','') + case when Division is not null then '1' else '0' endFROM(Select Distinct ShortState + ',' + Division as Division from table1 where ShortState=@State) tbl PRINT @OutputStringi am retrieving 1,1But i want 1,1,0,0can u please help me for this?Visa.G |
 |
|
|
nigelrivett
Master Smack Fu Yak Hacker
3385 Posts |
Posted - 2010-11-26 : 09:58:08
|
| I added a second query if that was the case - must have missed the deadlineAnd here it is again but without the mistake (or at least one of them) DECLARE @State VARCHAR(50)SET @State ='CA'Declare @OutputString varchar(max)SELECT @OutputString = coalesce(@OutputString + ',','') + case when Division is not null then '1' else '0' endFROM(select(Select i = 1 union all select 2 union all select 3 union all select 4) aleft joinTable1 ton t.division = a.iand ShortState=@State) tbl==========================================Cursors are useful if you don't know sql.SSIS can be used in a similar way.Beer is not cold and it isn't fizzy. |
 |
|
|
visa123
Yak Posting Veteran
54 Posts |
Posted - 2010-11-26 : 10:19:58
|
| Hi Thanks for your reply.i had one more problemCA 1CA 1CA 2Sometimes it will 1 (1,2,3,4 ) in double (or more) times.So for i put distinct in above.But if i use this it thorws wrong result.if there is no duplicate division its working fine.Can you please help for this ?Visa.G |
 |
|
|
nigelrivett
Master Smack Fu Yak Hacker
3385 Posts |
Posted - 2010-11-26 : 10:28:07
|
| DECLARE @State VARCHAR(50)SET @State ='CA'Declare @OutputString varchar(max)SELECT @OutputString = coalesce(@OutputString + ',','') + case when Division is not null then '1' else '0' endFROM(select(Select i = 1 union all select 2 union all select 3 union all select 4) aleft join(select distinct division from Table1 where ShortState=@State) ton t.division = a.i) tbl==========================================Cursors are useful if you don't know sql.SSIS can be used in a similar way.Beer is not cold and it isn't fizzy. |
 |
|
|
visa123
Yak Posting Veteran
54 Posts |
Posted - 2010-11-26 : 10:49:35
|
| Thank you very much.Its working fine.Visa.G |
 |
|
|
|
|
|
|
|