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 |
|
aakcse
Aged Yak Warrior
570 Posts |
Posted - 2012-06-26 : 11:54:57
|
I have a table Raw as belowHostName UserName---------------------------------------AIX2 actuser1+ actuser2S9 actuser4,actuser8,actuser12Want to get the table data re-arrange as belowHostName UserName-------------------------AIX2 actuser1+ actuser2S9 actuser4S9 actuser8S9 actuser12 some thing as belowINSERT INTO RawSELECT t.HostName, m.UserName AS MembersFROM Raw tCROSS APPLY (SELECT [UserName] FROM StringToTable(',',t.members) )m-Neil |
|
|
aakcse
Aged Yak Warrior
570 Posts |
Posted - 2012-06-26 : 12:06:43
|
| I have a function StringToTable as belowCREATE FUNCTION StringToTable (@Delimiter VARCHAR(5), @String VARCHAR(max))RETURNS @RtnValue TABLE ([ID] INT IDENTITY(1, 1),[Value] VARCHAR(max))AS BEGIN DECLARE @Cnt INTSET @Cnt=1WHILE (CHARINDEX(@Delimiter, @String)>0) BEGININSERT INTO @RtnValue ([Value])SELECT [Value] = LTRIM(RTRIM(SUBSTRING(@String, 1, CHARINDEX(@Delimiter, @String)-1)))SET @String=SUBSTRING(@String, CHARINDEX(@Delimiter, @String)+1, LEN(@String))SET @Cnt=@Cnt+1ENDINSERT INTO @RtnValue ([Value])SELECT [Value] = LTRIM(RTRIM(@String))RETURNEND-Neil |
 |
|
|
xhostx
Constraint Violating Yak Guru
277 Posts |
Posted - 2012-06-26 : 12:16:10
|
| What's your problem then?--------------------------Get rich or die trying-------------------------- |
 |
|
|
aakcse
Aged Yak Warrior
570 Posts |
Posted - 2012-06-26 : 12:34:29
|
I am getting the results as belowAIX2 actuser1+ actuser2S9 actuser4,actuser8,actuser12S9 actuser4,actuser8,actuser12S9 actuser4,actuser8,actuser12 -Neil |
 |
|
|
aakcse
Aged Yak Warrior
570 Posts |
Posted - 2012-06-26 : 12:42:23
|
| Got the resultsAfter changing the query as belowSELECT t.HostName, m.UserName AS MembersFROM raw tCROSS APPLY (SELECT Value as UserName FROM StringToTable(',',t.UserName) )m-Neil |
 |
|
|
|
|
|
|
|