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 |
Tep
Starting Member
2 Posts |
Posted - 2013-03-25 : 09:00:15
|
Hi,I am trying to change a table like this:ID code287 24;41295 12;3;22954 2;1into this:ID code287 24287 41295 12295 3295 22954 2954 1How should I do this? Maybe first I should pull apart the values from column code into more columns and thereafter something with a Pivot? I hope someone can help me with this.Thank you in advance,Tep |
|
bandi
Master Smack Fu Yak Hacker
2242 Posts |
Posted - 2013-03-25 : 09:55:42
|
[code]SELECT ID, sFROM YourtableOUTER APPLY CustomSplit(';', Codes) -- Codes = Your column name in the Table--[dbo].[CustomSplit] functionSET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGOCREATE FUNCTION [dbo].[CustomSplit] (@sep char(1), @s varchar(4000))RETURNS tableASRETURN (WITH Pieces(n, start, stop) AS (SELECT 1, 1, CHARINDEX(@sep, @s)UNION ALLSELECT n + 1, stop + 1, CHARINDEX(@sep, @s, stop + 1)FROM PiecesWHERE stop > 0)SELECT n,SUBSTRING(@s, start, CASE WHEN stop > 0 THEN stop-start ELSE 4000 END) AS sFROM Pieces)[/code] |
|
|
Tep
Starting Member
2 Posts |
Posted - 2013-03-25 : 10:19:10
|
Great! Thank you very much, Bandi! :-) |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
|
|
|
|
|
|