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)
 Question about substrings

Author  Topic 

andryi
Starting Member

17 Posts

Posted - 2009-10-07 : 11:09:02
How can divide one string in many substrings, I mean
If I have a string like this : 'abc,def,hij,'
how can obtain this:
abc
def
hij

rajdaksha
Aged Yak Warrior

595 Posts

Posted - 2009-10-07 : 11:14:12
Hi

Try this....



DECLARE @SPLIT_DATA VARCHAR(100)--size is upto u

SET @SPLIT_DATA = 'abc,def,hij'

SELECT @SPLIT_DATA= 'SELECT '''+REPLACE(@SPLIT_DATA,',',''' UNION ALL SELECT ''')+''''

EXEC(@SPLIT_DATA)




-------------------------
R...
Go to Top of Page

andryi
Starting Member

17 Posts

Posted - 2009-10-07 : 11:22:34
OMG!!, is perfect!!!! very thank you rajdaksha
Go to Top of Page

andryi
Starting Member

17 Posts

Posted - 2009-10-07 : 11:47:17
but and If I want to that each substring be each variable, like this:
declare @string1 varchar(50),@string2 varchar(50), @string3 varchar(50)
set @string1 = 'abc',
set @string2 = 'def',
set @string3 = 'hij'

Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2009-10-08 : 04:31:44
quote:
Originally posted by andryi

but and If I want to that each substring be each variable, like this:
declare @string1 varchar(50),@string2 varchar(50), @string3 varchar(50)
set @string1 = 'abc',
set @string2 = 'def',
set @string3 = 'hij'




What is the maximum number of possible values you would have?

Madhivanan

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

- Advertisement -