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 |
|
pradeep_iete
Yak Posting Veteran
84 Posts |
Posted - 2008-10-06 : 04:29:01
|
| hi I have rocrd in particular table T as ID name 1 pradeep, Vinay My output should look like pradeep vinay |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-10-06 : 04:34:21
|
use parsename functionSELECT t.ID,f.ValFROm YourTable tCROSS APPLY dbo.ParseValues(t.name)f parsevalues can be found belowCREATE FUNCTION ParseValues (@String varchar(8000) ) RETURNS @RESULTS TABLE (ID int identity(1,1), Val varchar(100) ) AS BEGIN DECLARE @Value varchar(100) WHILE @String is not null BEGIN SELECT @Value=CASE WHEN CHARINDEX(',',@String) >0 THEN LEFT(@String,CHARINDEX(',',@String)-1) ELSE @String END, @String=CASE WHEN CHARINDEX(',',@String) >0 THEN SUBSTRING(@String,CHARINDEX(',',@String)+1,LEN(@String)) ELSE NULL END INSERT INTO @RESULTS (Val) SELECT @Value END RETURN END |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-10-06 : 04:40:19
|
example declare @test table(ID int,name varchar(8000))insert into @testselect 1, 'pradeep,Vinay' union allselect 2, 'Sanjay,Moti,Manu' union allselect 3, 'Kiran' union allselect 4, 'Rahul,Parveen,Harilal' union allselect 5, 'Mohan,Ram,Rita,Mary,Sita'select t.id,f.valfrom @test tcross apply dbo.parsevalues(t.name) f output-------------------------------------------------------id name----------- ---------------------------------------------------------1 pradeep1 Vinay2 Sanjay2 Moti2 Manu3 Kiran4 Rahul4 Parveen4 Harilal5 Mohan5 Ram5 Rita5 Mary5 Sita |
 |
|
|
raky
Aged Yak Warrior
767 Posts |
Posted - 2008-10-06 : 07:17:12
|
| We can use this method alsodeclare @test table(ID int,name varchar(8000))insert into @testselect 1, 'pradeep,Vinay' union allselect 2, 'Sanjay,Moti,Manu' union allselect 3, 'Kiran' union allselect 4, 'Rahul,Parveen,Harilal' union allselect 5, 'Mohan,Ram,Rita,Mary,Sita'SELECT t.id, SUBSTRING(t.name, v.Number - 1, COALESCE(NULLIF(CHARINDEX(',', t.name, v.Number), 0), LEN(t.name) + 1) - v.Number + 1) AS nameFROM @test AS tINNER JOIN master..spt_values AS v ON v.Type = 'p'WHERE SUBSTRING(',_' + t.name, v.Number, 1) = ',' |
 |
|
|
|
|
|
|
|