| Author |
Topic |
|
hai
Yak Posting Veteran
84 Posts |
Posted - 2008-06-04 : 15:06:35
|
| I have the following:id docid groupid Option48 9861 1 123535 3973 3 3423 14820 1 12346would like to see this result:id docid groupid Option48 9861 1 148 9861 1 248 9861 1 348 9861 1 535 3973 3 335 3973 3 423 14820 1 123 14820 1 223 14820 1 323 14820 1 423 14820 1 6thanks you |
|
|
TG
Master Smack Fu Yak Hacker
6065 Posts |
Posted - 2008-06-04 : 15:11:31
|
Here's one way (assuming Option is varchar)declare @t table (id int, docid int, groupid int, [Option] varchar(10))insert @tselect 48, 9861, 1, '1235' union allselect 35, 3973, 3, '34' union allselect 23, 14820, 1, '12346'select t.id, t.docid, t.groupid, substring(t.[option], v.number, 1) [Option]from @t tcross join master..spt_values vwhere v.type = 'p'and v.number > 0and v.number <= len(t.[option])output:id docid groupid Option----------- ----------- ----------- ------48 9861 1 148 9861 1 248 9861 1 348 9861 1 535 3973 3 335 3973 3 423 14820 1 123 14820 1 223 14820 1 323 14820 1 423 14820 1 6 Be One with the OptimizerTG |
 |
|
|
hai
Yak Posting Veteran
84 Posts |
Posted - 2008-06-04 : 15:54:25
|
| thanks.... |
 |
|
|
raky
Aged Yak Warrior
767 Posts |
Posted - 2008-06-05 : 09:08:32
|
| try this one also DECLARE @TAB table( id INT, docid INT, groupid INT, op VARCHAR(10))INSERT INTO @tab SELECT 49,2322,1,'1234' UNION allSELECT 50,3234,2,'34' UNION allSELECT 51,3235,3,'12'SELECT id,docid,groupid, (SELECT(SUBSTRING(op,s.number+1,1))) AS opFROM @tab t INNER JOINmaster..spt_values s ON S.TYPE = 'P'where (SUBSTRING(op,s.number+1,1)) != '' |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2008-06-05 : 09:42:27
|
Why not this?DECLARE @Sample TABLE (id INT, docid INT, groupid INT, [Option] VARCHAR(10))INSERT @SampleSELECT 48, 9861, 1, '1235' UNION ALLSELECT 35, 3973, 3, '34' UNION ALLSELECT 23, 14820, 1, '12346';WITH Yak (id, docid, groupid, [option], pos, chr)AS ( SELECT id, docid, groupid, [option], 1, LEFT([option], 1) FROM @Sample UNION ALL SELECT id, docid, groupid, [option], Pos + 1, SUBSTRING([option], Pos + 1, 1) FROM Yak WHERE Pos < LEN([option]))SELECT id, docid, groupid, chrFROM YakORDER BY id, pos E 12°55'05.25"N 56°04'39.16" |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2008-06-05 : 09:57:33
|
Or this-- Peso 2SELECT p.id, p.docid, p.groupid, p.[option]FROM ( SELECT id, docid, groupid, NULLIF(SUBSTRING([option], 1, 1), '') AS Col1, NULLIF(SUBSTRING([option], 2, 1), '') AS Col2, NULLIF(SUBSTRING([option], 3, 1), '') AS Col3, NULLIF(SUBSTRING([option], 4, 1), '') AS Col4, NULLIF(SUBSTRING([option], 5, 1), '') AS Col5, NULLIF(SUBSTRING([option], 6, 1), '') AS Col6, NULLIF(SUBSTRING([option], 7, 1), '') AS Col7, NULLIF(SUBSTRING([option], 8, 1), '') AS Col8, NULLIF(SUBSTRING([option], 9, 1), '') AS Col9, NULLIF(SUBSTRING([option], 10, 1), '') AS Col10 FROM @Sample ) AS sUNPIVOT ( [option] FOR theCol IN (s.Col1, s.Col2, s.Col3, s.Col4, s.Col5, s.Col6, s.Col7, s.Col8, s.Col9, s.Col10) ) AS p Results: RowCounts CPU Duration Reads Writes --------- ------ -------- --------- ------TG 655,360 1,048 19,349 196,833 0Raky 655,360 64,344 138,753 817,747 0Peso 1 655,360 11,657 36,725 6,845,649 62Peso 2 655,360 672 17,971 224 0 E 12°55'05.25"N 56°04'39.16" |
 |
|
|
|
|
|