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.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 SELECT 'aaa,bbb,ccc,ddd'

Author  Topic 

R@bb1t
Starting Member

28 Posts

Posted - 2008-01-18 : 07:43:51
I have this select statement

SELECT '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
Go to Top of Page

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 d

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

R@bb1t
Starting Member

28 Posts

Posted - 2008-01-18 : 08:06:04
Nah, not this way... by coding..
Go to Top of Page

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?

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

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.
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2008-01-18 : 09:11:45
http://vyaskn.tripod.com/passing_arrays_to_stored_procedures.htm

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

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 use
DECLARE @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
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2008-01-18 : 09:38:18
use the function here http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=76033


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

R@bb1t
Starting Member

28 Posts

Posted - 2008-01-21 : 06:24:12
Thx a lot... it saved my time :)
Cheers!
Go to Top of Page
   

- Advertisement -