I have a string to be split; it has a format like this:
---------------------------------------------------------
HEADING DETAILS HERE:
~
INVOICE1^VALUE1^VALUE2^
INVOICE2^VALUE1^VALUE2^
INVOICE3^VALUE1^VALUE2^
TOTAL^OFVALUE1^OFVALUE2^
~
FOOTER DETAILS HERE
---------------------------------------------------------
My output should be a table with 3 columns:
| INVOICE | VALUE1 | VALUE2 |
INVOICE 1---------- VALUE1 ---------- VALUE2
INVOICE 2---------- VALUE1 ---------- VALUE2
INVOICE 3---------- VALUE1 ---------- VALUE2
My idea is to split with the first delimiter ~ and split 2 with delimiter ^
I made a split function and this is the result of my first split with delimiter ~
ROW 1 HEADER DETAILS HERE:
ROW 2 INVOICE1^VALUE1^VALUE2^ INVOICE2^VALUE1^VALUE2^ INVOICE3^VALUE1^VALUE2^
ROW 3 FOOTER DETAILS HERE
BUT, what I need to do is to put the results into columns instead of rows. ALSO, I need to get the result of ROW 2 and split it again and put it in columns like this:
INVOICE column - VALUE1 column - VALUE2 column
INVOICE 1---------- VALUE1 ---------- VALUE2
INVOICE 2---------- VALUE1 ---------- VALUE2
INVOICE 3---------- VALUE1 ---------- VALUE2
I hope somebody will be able to read this question and help me in the coding of my TSQL.
FOLLOW UP QUESTION: 8/13/2013
I tried to alter Rs' code to something like this...
USE [test]
GO
/****** Object: StoredProcedure [dbo].[SplitSP4] Script Date: 8/13/2013 7:33:31 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author: <Author,,Name>
-- Create date: <Create Date,,>
-- Description: <Description,,>
-- =============================================
ALTER PROCEDURE [dbo].[SplitSP4]
-- Add the parameters for the stored procedure here
--<@Param1, sysname, @p1> <Datatype_For_Param1, , int> = <Default_Value_For_Param1, , 0>,
--<@Param2, sysname, @p2> <Datatype_For_Param2, , int> = <Default_Value_For_Param2, , 0>
@TABLE_id varchar(50)
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
--SET NOCOUNT ON;
declare @string varchar(8000)
declare @stringXML xml
declare @NewLine char(2)
set @NewLine=char(13)+char(10)
set @string = (SELECT [dbo].[TABLE_NAME].[iam_string] FROM [dbo].[TABLE_NAME] WHERE [dbo].[TABLE_NAME].[id] = @TABLE_id)
--'HEADING DETAILS HERE:
--~
--INVOICE1^VALUE1^VALUE2^
--INVOICE2^VALUE1^VALUE2^
--INVOICE3^VALUE1^VALUE2^
--TOTAL^OFVALUE1^OFVALUE2^
--~
--FOOTER DETAILS HERE'
declare @table1 table(cols varchar(max))
--convert string to xml
SELECT @stringXML = CONVERT(xml,'<root><i>' +
Replace(@string, @NewLine, '</i><i>') + '</i></root>')
--split xml string into different rows
insert into @table1
select Cols = I.c.value('.', 'varchar(MAX)')
from @stringXML.nodes('/root/i') I(c)
--split invoice rows into columns
select
Inv.value('/Invoice[1]/Attribute[1]','varchar(50)') AS [Invoice],
Inv.value('/Invoice[1]/Attribute[2]','varchar(50)') AS [Value1],
Inv.value('/Invoice[1]/Attribute[3]','varchar(50)') AS [Value2]
from
(
select CONVERT(XML,'<Invoice><Attribute>'
+ REPLACE([cols],'~', '</Attribute><Attribute>')
+ '</Attribute></Invoice>') Inv
from @table1 where cols like '%^%'
) x
end
...for me to be able to make a STORED PROCEDURE for a CRYSTAL REPORT. But it seems that when I executed this STORED PROCEDURE it returned this error.
Msg 9421, Level 16, State 1, Procedure SplitSP4, Line 34
XML parsing: line 1, character 31, illegal name character
I wonder how am I goint to fix my code to fit the sample XML code?