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 |
|
R@bb1t
Starting Member
28 Posts |
Posted - 2008-01-18 : 07:43:51
|
| I have this select statementSELECT 'aaa,bbb,ccc,ddd'the result is given in 1 column.. How can I get it in 4 different columns??? |
|
|
Grinja
Starting Member
5 Posts |
Posted - 2008-01-18 : 07:49:09
|
| SELECT aaa AS A, bbb AS B, etc |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2008-01-18 : 07:54:30
|
| SELECT 'aaa' as a,'bbb' as b,'ccc' as c,'ddd' as dMadhivananFailing to plan is Planning to fail |
 |
|
|
R@bb1t
Starting Member
28 Posts |
Posted - 2008-01-18 : 08:06:04
|
| Nah, not this way... by coding.. |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2008-01-18 : 08:20:21
|
quote: Originally posted by R@bb1t Nah, not this way... by coding..
What do you mean?MadhivananFailing to plan is Planning to fail |
 |
|
|
singularity
Posting Yak Master
153 Posts |
Posted - 2008-01-18 : 09:06:41
|
| I believe he's trying to parse a comma-separated string into separate columns. If each value has a fixed length, then you can just use the substring function to extract the values: (e.g. select substring(string,1,3) as col1, substring(string,5,7) as col2, etc.If they are not fixed length, you'll have to use the charindex function to determine the position of the comma and then use the substring function. |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2008-01-18 : 09:11:45
|
| http://vyaskn.tripod.com/passing_arrays_to_stored_procedures.htmMadhivananFailing to plan is Planning to fail |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-01-18 : 09:12:59
|
| you have only 4 comma seperated values always? Then useDECLARE @Str varchar(100)SELECT @Str='aaa,bbb,ccc,ddd'SELECT PARSENAME(REPLACE(@Str,',','.'),4) AS FirstCol,PARSENAME(REPLACE(@Str,',','.'),,3) AS SecondCol,PARSENAME(REPLACE(@Str,',','.'),,2) AS ThirdCol,PARSENAME(REPLACE(@Str,',','.'),,1) AS FourthCol |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
|
|
R@bb1t
Starting Member
28 Posts |
Posted - 2008-01-21 : 06:24:12
|
| Thx a lot... it saved my time :)Cheers! |
 |
|
|
|
|
|
|
|