Could not able to insert the xml data into a table - sql-server

When i try to insert the values into the table am getting the below error.
We cant remove the space in the XML because it is generated from Javascript.
How to insert the below data into the XMLdata table.
Conversion failed when converting date and/or time from character string.
This is the sample data(#bbhdn5):
341300-02-1|04/10/2018 01:18:29|04/10/2018 06:18:29|133072;
261600-01-1|04/10/2018 06:18:29|04/10/2018 11:18:29|133073;
781100-R1-1|04/10/2018 11:18:29|04/10/2018 16:18:29|133074;
Code:
create table WMC_Savexmldata
(
XML nvarchar(max)
)
Declare #bbhdn5 nvarchar(max)
set #bbhdn5='341300-02-1|04/10/2018 01:18:29|04/10/2018 06:18:29|133072; 261600-01-1|04/10/2018 06:18:29|04/10/2018 11:18:29|133073; 781100-R1-1|04/10/2018 11:18:29|04/10/2018 16:18:29|133074;'
insert into WMC_Savexmldata
select #bbhdn5

Although this question is absolutely misleading, my magic crystal ball started to blink suddenly and told me, that you might be looking for this:
Replacing the delimiters ; and | allows to transfer your CSV-string to this XML:
<x>
<y>341300-02-1</y>
<y>04/10/2018 01:18:29</y>
<y>04/10/2018 06:18:29</y>
<y>133072</y>
</x>
<x>
<y> 261600-01-1</y>
<y>04/10/2018 06:18:29</y>
<y>04/10/2018 11:18:29</y>
<y>133073</y>
</x>
<x>
<y> 781100-R1-1</y>
<y>04/10/2018 11:18:29</y>
<y>04/10/2018 16:18:29</y>
<y>133074</y>
</x>
I use this to get the data as derived table:
DECLARE #bbhdn5 NVARCHAR(MAX);
SET #bbhdn5=N'341300-02-1|04/10/2018 01:18:29|04/10/2018 06:18:29|133072; 261600-01-1|04/10/2018 06:18:29|04/10/2018 11:18:29|133073; 781100-R1-1|04/10/2018 11:18:29|04/10/2018 16:18:29|133074;';
WITH Splitted AS
(
SELECT CAST('<x><y>' + REPLACE(REPLACE(#bbhdn5,'|','</y><y>'),';','</y></x><x><y>') + '</y></x>' AS XML) AS Casted
)
SELECT ROW_NUMBER() OVER(ORDER BY (SELECT NULL)) AS RowNumber
,A.x.value('y[1]','nvarchar(max)') AS RowCode
,CONVERT(DATETIME,A.x.value('y[2]','nvarchar(max)'),103) AS Date1
,CONVERT(DATETIME,A.x.value('y[3]','nvarchar(max)'),103) AS Date2
,A.x.value('y[4]','int') AS SomeNumber
FROM Splitted
CROSS APPLY Casted.nodes('/x[y/text()]') AS A(x);
The result
+-----------+-------------+-------------------------+-------------------------+------------+
| RowNumber | RowCode | Date1 | Date2 | SomeNumber |
+-----------+-------------+-------------------------+-------------------------+------------+
| 1 | 341300-02-1 | 2018-10-04 01:18:29.000 | 2018-10-04 06:18:29.000 | 133072 |
+-----------+-------------+-------------------------+-------------------------+------------+
| 2 | 261600-01-1 | 2018-10-04 06:18:29.000 | 2018-10-04 11:18:29.000 | 133073 |
+-----------+-------------+-------------------------+-------------------------+------------+
| 3 | 781100-R1-1 | 2018-10-04 11:18:29.000 | 2018-10-04 16:18:29.000 | 133074 |
+-----------+-------------+-------------------------+-------------------------+------------+
UPDATE
Change the line within the CTE Splitted to this
SELECT CAST('<x><y>' + REPLACE(REPLACE(REPLACE(REPLACE(#bbhdn5,CHAR(10),' '),CHAR(13),' '),'|','</y><y>'),';','</y></x><x><y>') + '</y></x>' AS XML) AS Casted
This will replace CHAR(13) and CHAR(10) with blanks. Any odd line break within your input should disappear. Well, you might have some additional blanks in string values. But you can replace doubled blanks with single blanks again...

insert into WMC_Savexmldata
select '496200-01-1|03/31/2018 11:18:29|03/31/2018 16:18:29|133015;245000-01-1|03/31/2018
16:18:29|03/31/2018 21:18:29|133017;262100-13-1|03/31/2018 21:18:29|04/01/2018 02:18:29|133018;'
Please insert the data like this(attached). Date comes in one line and time comes in another line and then run the below query. You will be getting the date time conversion error because of the space error in between date and time. I just want to breaks between the dates. As it is coming from browser the date is coming with space in the SQL. Please help on this. Are you clear with my question.
Declare #InputSepTmp table
(
id int,
Inputs nvarchar(max)
)
insert into #InputSepTmp
Select Row_Number() over (Order By (Select null))
, LTrim(RTrim(B.i.value('(./text())1', 'varchar(max)')))
From (Select x = Cast('' + replace((Select replace(XML,';','§§Split§§') as [*] For XML Path('')),'§§Split§§','')+'' as xml).query('.')
from WMC_Savexmldata) as A
Cross Apply x.nodes('x') AS B(i)
-- select * from #InputSepTmp
--- Cursor --------------------
SET NOCOUNT ON
DECLARE #InputID varchar(200)
DECLARE cur_InputSeparator CURSOR
STATIC FOR
select id from #InputSepTmp where Inputs <> ''
OPEN cur_InputSeparator
IF ##CURSOR_ROWS > 0
BEGIN
FETCH NEXT FROM cur_InputSeparator INTO #InputID
WHILE ##Fetch_status = 0
BEGIN
DEclare #FinalInputtmp table
(
id int,
IPValues varchar(100)
)
insert into #FinalInputtmp
-- SELECT
-- Split.a.value('.', 'NVARCHAR(max)') AS String
--FROM (SELECT
-- CAST ('' + REPLACE(LTRIM(RTRIM(Inputs)), ',', '') + '' AS XML) AS String
-- from #InputSepTmp T1 where id=#InputID) AS A CROSS APPLY String.nodes ('/M') AS Split(a);
Select Row_Number() over (Order By (Select null))
, LTrim(RTrim(B.i.value('(./text())1', 'varchar(max)')))
From (Select x = Cast('' + replace((Select replace(LTRIM(RTRIM(Inputs)),'|','§§Split§§') as [*] For XML Path('')),'§§Split§§','')+'' as xml).query('.')
from #InputSepTmp where id = #InputID) as A
Cross Apply x.nodes('x') AS B(i)
--select convert(datetime,'04/12/2018 12:50:08')
insert into WMC_CriticalPath_ScheduledDtls (SWOPACKAGENO,TASKNO,SCHEDULEDSTDATE,SCHEDULEDENDDATE,TRACKID,CreatedDate,ModifiedDate)
SELECT 'adjb', MAX(CASE WHEN D.RN=1 THEN LTRIM(RTRIM(D.IPValues)) END)[task no]
,MAX(CASE WHEN D.RN=2 THEN LTRIM(RTRIM(D.IPValues)) END) [start date]
,MAX(CASE WHEN D.RN=3 THEN LTRIM(RTRIM(D.IPValues)) END) [end date]
,MAX(CASE WHEN D.RN=4 THEN LTRIM(RTRIM(D.IPValues)) END) [id], Getdate(),NULL
FROM(
SELECT *
,ROW_NUMBER() OVER(ORDER BY (SELECT NULL))RN
FROM #FinalInputtmp
)D
delete from #FinalInputtmp
FETCH NEXT FROM cur_InputSeparator INTO #InputID
END
END
CLOSE cur_InputSeparator
DEALLOCATE cur_InputSeparator
SET NOCOUNT OFF
--select * from WMC_CriticalPath_ScheduledDtls
select * from WMC_CriticalPath_ScheduledDtls

Related

Split string using delimiter that the SSRS passes - select multiple values from the parameter

I have this stored procedure where I am trying to select multiple values from the parameter Bkt. This works okay when I select one value from Bkt but Bkt has different values that I want to be able to select from in the SSRS.
For example in this screenshot you can see that there are CFI and DPR inside of Bkt. I want to be able to select both CFI and DPR in Bkt.
I need to keep the stored procedure so I am trying to split the delimited string that SSRS passes. I can't figure it out. I tried changing the delimiter by using an expression in the Parameters mapping section but I couldn't figure that out either. I am just so lost.
This is my Stored Procedure
#Press varchar(10),
#BKT varchar (10)
AS
BEGIN
SET NOCOUNT ON;
select * From (
Select FAC,SPEC_NEW.Tread_Code, Case When cosw.tread_code = spec_new.tread_code Then 'Scheduled' else 'Alternative' end AS Size, COUNT(Distinct loc.serial ) QTY ,
ROW_NUMBER() OVER (
PARTITION BY FAC, SPEC_NEW.Tread_Code
ORDER BY fac, Case When cosw.tread_code = spec_new.tread_code Then 'Scheduled' else 'Alternative' end DESC, SPEC_NEW.Tread_Code
) AS r_num
FROM [TireTrack].[dbo].[cos_work] cosw with (nolock)
Inner Join [SharedData].dbo.spec_master Spec with (nolock) On spec.spec=Cosw.SPEC
Inner Join [SharedData].dbo.spec_master SPEC_NEW with (nolock) On SPEC_NEW.ARTICLE=SPEC.article
Inner Join [DataWarehouse].[dbo].[Locator] LOC with (Nolock) ON LOC.SPEC=SPEC_NEW.SPEC
Where Cosw.FAC=#press and Loc.BKT = #bkt
GROUP BY FAC, cosw.Tread_Code, SPec_new.Tread_Code, Loc.Bkt
) as a
where r_num=1
order by FAC
END
I have another Stored Procedure where I am getting Press and Bkt
AS
BEGIN
SET NOCOUNT ON;
Select distinct FAC as Press, Loc.BKT as Bkt
FROM [TireTrack].[dbo].[cos_work], [DataWarehouse].[dbo].[Locator] LOC
END
I was using this link as a reference
Here is a conceptual example for you how to handle comma separated parameter to a stored procedure.
SQL
DECLARE #tbl TABLE (ID INT IDENTITY PRIMARY KEY, vehicleMake VARCHAR(20));
INSERT INTO #tbl (vehicleMake) VALUES
('Chevrolet'),
('Tesla'),
('Audi'),
('Nissan');
DECLARE #ParameterList VARCHAR(max) = '1,2';
-- Method #1
-- XML and XQuery
-- SQL Server 2008 onwards
DECLARE #separator CHAR(1) = ',';
;WITH rs AS
(
SELECT xmldata = TRY_CAST('<root><r>' +
REPLACE(#ParameterList, #separator, '</r><r>') + '</r></root>' AS XML)
)
SELECT tbl.*
FROM rs CROSS APPLY xmldata.nodes('/root/r/text()') AS t(c)
INNER JOIN #tbl AS tbl ON tbl.id = c.value('.','INT');
-- Method #2
-- STRING_SPLIT()
-- SQL Server 2016 onwards
SELECT tbl.*
FROM #tbl AS tbl INNER JOIN
STRING_SPLIT(#ParameterList, ',') AS ss
ON tbl.ID = ss.value;
Output
+----+-------------+
| ID | vehicleMake |
+----+-------------+
| 1 | Chevrolet |
| 2 | Tesla |
+----+-------------+

filter out all datetime and integer values from a column in SQL SERVER

I have a string
04/09/2018 06:21:38 101342 CHARLESD JOHNSON:713-269-1878 CALL WHEN WE GET A PO 06/09/2018 08:41:38 101345 KHARLESD KOHNSON:813-269-1878 CALL WHEN WE GET A PO 08/09/2018 09:41:38 10356 THARLESD TOHNSON:913-269-1878 CALL WHEN WE GET A PO
I want output like
DateTime1 | EmpID
04/09/2018 06:21:38 101342
06/09/2018 08:41:38 101345
08/09/2018 09:41:38 10356
Please help
Create the function PatternSplitLoop from this awesome article:
Splitting Strings Based on Patterns
and execute the following:
declare #tab table (string varchar(max))
insert into #tab select '04/09/2018 06:21:38 101342 CHARLESD JOHNSON:713-269-1878 CALL WHEN WE GET A PO 06/09/2018 08:41:38 101345 KHARLESD KOHNSON:813-269-1878 CALL WHEN WE GET A PO 08/09/2018 09:41:38 10356 THARLESD TOHNSON:913-269-1878 CALL WHEN WE GET A PO '
select left(item, 19) DateTime1, substring(item, 20, len(item)) EmpID
from #tab t
cross apply [dbo].[PatternSplitLoop](string, '%[0-9][0-9][/][0-9][0-9][/][0-9][0-9][0-9][0-9]%') f
where matched = 1
Output:
XML split version:
DECLARE #Val NVARCHAR(MAX) = '04/09/2018 06:21:38 101342 CHARLESD JOHNSON:713-269-1878 CALL WHEN WE GET A PO 06/09/2018 08:41:38 101345 KHARLESD KOHNSON:813-269-1878 CALL WHEN WE GET A PO 08/09/2018 09:41:38 10356 THARLESD TOHNSON:913-269-1878 CALL WHEN WE GET A PO '
SELECT #Val = '<a>' + REPLACE(#Val, ' ', '</a><a>') + '</a>';
DECLARE #Xml XML = CONVERT(XML, #Val);
DECLARE #ValTable TABLE
(
ROWNUM INT IDENTITY(1, 1),
Val NVARCHAR(MAX)
)
INSERT into #ValTable
(Val)
SELECT *
FROM
(
SELECT c.value('.', 'NVARCHAR(64)') AS Val
FROM #Xml.nodes('/a') T(c)
) a
WHERE LEN(Val) <> 0
AND (TRY_CONVERT(DATETIME, Val) IS NOT NULL OR TRY_CONVERT(TIME, Val) IS NOT NULL OR TRY_CONVERT(INT, Val) IS NOT NULL);
SELECT CONVERT(DATETIME, CONCAT(a.Val, ' ', b.Val)) AS DateTime1, c.Val AS EmpId
FROM #ValTable a
JOIN #ValTable b
--Date and Time rows, B is time row
ON TRY_CONVERT(TIME, a.Val) IS NOT NULL AND TRY_CONVERT(TIME, b.Val) IS NOT NULL AND b.ROWNUM = a.ROWNUM + 1
-- Int rows
JOIN #ValTable c
ON TRY_CONVERT(INT, c.Val) IS NOT NULL AND c.ROWNUM = a.RowNum + 2;

Split a separated value to new single column

I want to separate the | delimited string into new column, I tried the below code but I think is not a good practice.
SELECT
REPLACE(SUBSTRING(ReferenceName, 1, CHARINDEX('|', ReferenceName)), '|', '') AS CreditCard,
ReferenceName
FROM
CTS.DBO.cts_TxSalesPayment
Expected result
Split the value based on the delimiter:
CardNumber | ExpiryDate | ApprovalCode | .... | ... | ... | ...
One way would be to use a REPLACE function to allow the column to be parsed as XML, then PIVOT the result.
For example:
DECLARE #T TABLE (ReferenceName VARCHAR(MAX));
INSERT #T VALUES ('CardNumber=asdf|ExpiryDate=1234124x|ApprovalCode=aaaaa'), ('CardNumber=zzz|ExpiryDate=123|ApprovalCode=q'), ('CardNumber=zzz|ExpiryDate=111|ApprovalCode=q2');
SELECT *
FROM
(
SELECT RN, colName = A.B.value('local-name(.)', 'varchar(max)'), colVal = A.B.value('#val', 'varchar(max)')
FROM
(
SELECT RN = ROW_NUMBER() OVER (ORDER BY (SELECT NULL)), col = CAST('<' + REPLACE(REPLACE(ReferenceName, '=', ' val='''), '|', '''/><') + ''' />' AS XML)
FROM #T
) AS T
CROSS APPLY col.nodes('*') AS A(B)
) AS T
PIVOT
(
MAX(colVal) FOR colName IN ([CardNumber], [ExpiryDate], [ApprovalCode])
) AS P;

count and remove individual values of a single column

I have a table and the values like this
create table items_table(url varchar(max),counttotal_urls int,countduplicate_urls int,Unique_urls varchar(max),
countUnique_urls int)
insert into items_table(url) values('ht,ha,hb,ha|hc|hy')
insert into items_table(url) values('ht,hb,hb|hb|hx|hx')
insert into items_table(url) values('hz,hy,hx,hm|hm,hy')
insert into items_table(url) values('hz,hy,hx,hm|hm,hy')
I need to replace ,h with |h , for this I will use replace(url,',h','|h')
I need to count total_urls present by considering the separation '|'
I want to check or count how may duplicate url's are present
I want unique_urls to be updated in the Unique_url's column by removing the duplicates
Finally I want to count the Unique_urls which will be updated in another column
Desired output:
This is a bit complicated. But I tried to achieve it in Set based methodology.
Your Schema:
CREATE TABLE #items_table (
id INT identity
,url VARCHAR(max)
,counttotal_urls INT
,countduplicate_urls INT
,Unique_urls VARCHAR(max)
,countUnique_urls INT
)
INSERT INTO #items_table (url)
VALUES ('ht,ha,hb,ha|hc|hy')
INSERT INTO #items_table (url)
VALUES ('ht,hb,hb|hb|hx|hx')
INSERT INTO #items_table (url)
VALUES ('hz,hy,hx,hm|hm,hy')
INSERT INTO #items_table (url)
VALUES ('hy,hx,hm|hm,hy')
I have used several CTE's and XML methods
;WITH CTE
AS (
SELECT url
,REPLACE(',' + url, ',h', '|h') AS url2
,CAST('<M>'
+ REPLACE(REPLACE(',' + url, ',h', '|h'), '|', '</M><M>')
+ '</M>' AS XML) AS XML_FLD
FROM #items_table
)
,CTE2
AS (
SELECT url
,SUM(CASE
WHEN SUBSTRING(url2, number, 1) > '|'
THEN 1
ELSE 0
END) / 2 AS counttotal_urls
FROM CTE C
CROSS APPLY (
SELECT *
FROM master.dbo.spt_values
WHERE type = 'P'
AND number BETWEEN 1
AND LEN(C.url2)
) CA
GROUP BY url
)
,CTE3
AS (
SELECT C2.url
,C2.counttotal_urls
,SPLITS.ABC.value('.', 'varchar(MAX)') DUP_URLS
FROM CTE2 C2
INNER JOIN CTE C ON C2.url = C.url
CROSS APPLY C.XML_FLD.nodes('/M') AS SPLITS(ABC)
)
SELECT url
,counttotal_urls
,counttotal_urls - (COUNT(DISTINCT DUP_URLS) - 1) AS countduplicate_urls
,STUFF((
SELECT DISTINCT '|' + DUP_URLS
FROM CTE3 C
WHERE C3.url = C.url
FOR XML PATH('')
), 1, 1, '') AS Unique_urls
FROM CTE3 C3
GROUP BY url
,counttotal_urls
Result will be
+-------------------+-----------------+---------------------+-----------------+
| url | counttotal_urls | countduplicate_urls | Unique_urls |
+-------------------+-----------------+---------------------+-----------------+
| ht,ha,hb,ha|hc|hy | 6 | 1 | |ha|hb|hc|ht|hy |
| ht,hb,hb|hb|hx|hx | 6 | 3 | |hb|ht|hx |
| hy,hx,hm|hm,hy | 5 | 2 | |hm|hx|hy |
| hz,hy,hx,hm|hm,hy | 6 | 2 | |hm|hx|hy|hz |
+-------------------+-----------------+---------------------+-----------------+
You will have to create a string split table value function (Found one at aspsnippets)
as below
CREATE FUNCTION ufn_SplitString
(
#Input NVARCHAR(MAX),
#Character CHAR(1)
)
RETURNS #Output TABLE (
Item NVARCHAR(1000)
)
AS
BEGIN
DECLARE #StartIndex INT, #EndIndex INT
SET #StartIndex = 1
IF SUBSTRING(#Input, LEN(#Input) - 1, LEN(#Input)) <> #Character
BEGIN
SET #Input = #Input + #Character
END
WHILE CHARINDEX(#Character, #Input) > 0
BEGIN
SET #EndIndex = CHARINDEX(#Character, #Input)
INSERT INTO #Output(Item)
SELECT SUBSTRING(#Input, #StartIndex, #EndIndex - 1)
SET #Input = SUBSTRING(#Input, #EndIndex + 1, LEN(#Input))
END
RETURN
END
GO
Once the Function is in place the below code can be used to achieve the desired result
;WITH cte_OriginalTable(url) as
(
SELECT 'ht,ha,hb,ha|hc|hy' UNION ALL
SELECT 'ht,hb,hb|hb|hx|hx' UNION ALL
SELECT 'hz,hy,hx,hm|hm,hy' UNION ALL
SELECT 'hz,hy,hx,hm|hm,hy'
)
,cte_SaperaterFix AS
(
SELECT ROW_NUMBER() OVER(ORDER BY (SELECT NULL)) AS ID, replace(url, ',h', '|h') AS url
FROM cte_OriginalTable
)
,cte_Split as
(
SELECT o.*,
y.Item
FROM cte_SaperaterFix o
CROSS APPLY dbo.ufn_SplitString(o.url, '|') y
)
,cte_TotalCount AS
(
SELECT ID,COUNT(ID) AS counttotal_urls, COUNT(DISTINCT Item) AS Unique_urls, COUNT(ID) - COUNT(DISTINCT Item) AS countduplicate_urls
FROM cte_Split
GROUP BY ID
)
SELECT DISTINCT b.ID, b.url AS URLs, a.CountTotal_URLs, a.CountDuplicate_URLS, STUFF(( SELECT DISTINCT '|' + b1.Item AS [text()]
FROM cte_Split b1
WHERE
b.ID = b1.ID
FOR XML PATH('')
), 1, 1, '' ) AS Unique_URLs, a.Unique_URLs AS CountUnique_URLs
FROM cte_TotalCount a
JOIN cte_Split b
ON a.ID = b.ID

Varchar value split in SQL

I have a column in a table
Table 1
Tid. name fid
-----------------
1. Abc. 233
2. def. 344
3. xyz. 455
Table 2
did. Status. name FID
-------------------------------------------
1 Open. Abu,def,xyz 233,344,455
Now I want to split these fid and name from table2 in a stored procedure like this:
ID status name FID
---------------
1. Open. Abc. 233
2. Open. Def. 344
3 Open. xyz. 455
Update stored procedure
Query:
Create procedure as splitdata
As
Begin
Declare #fid varchar (500)
Select name, select item as fid
from spiltstring(#fid,','))
from table1
Inner join table2 on table1.fid = table2.fid
This shows error
Conversion failed when converting the varchar value '233,344,455' to data type int
In table1 fid is int and in table2 fid is varchar type.
Split string function is
CREATE FUNCTION SplitString
(
#Input NVARCHAR(MAX),
#Character CHAR(1)
)
RETURNS #Output TABLE (Item NVARCHAR(1000))
AS
BEGIN
DECLARE #StartIndex INT, #EndIndex INT
SET #StartIndex = 1
IF SUBSTRING(#Input, LEN(#Input) - 1, LEN(#Input)) <> #Character
BEGIN
SET #Input = #Input + #Character
END
WHILE CHARINDEX(#Character, #Input) > 0
BEGIN
SET #EndIndex = CHARINDEX(#Character, #Input)
INSERT INTO #Output(Item)
SELECT SUBSTRING(#Input, #StartIndex, #EndIndex - 1)
SET #Input = SUBSTRING(#Input, #EndIndex + 1, LEN(#Input))
END
RETURN
END
GO
How can I do this?
UPDATE
Ok according to #john solution I try this solution ...
But as I have multiple other tables
Like
Create procedure as sp2
#tick int
As
Select
Table 4.column, Table 5.column, Table 3.column,
--so here I used solution like this
(Select b.fid
from table2 t2
cross apply
(Select fid = s2.retval
from [udf-str-parase] (t2.fid, ',') s2) b),
Table 6.column
From
Table 2
Inner join
table4 on table4.id = table5.id
..........
Where
tick = #tick
When I try to execute only cross apply query this shows perfect result
But when I try to execute whole stored procedure like this
Exec sp2 28
I get this error:
Subquery returned more than 1 value.this is not permitted. When the subquery follows =,!=,<=,>=,>,< or when the subquery iIs used an expression.
Any solutions?
The the help of a split/parse function and a CROSS APPLY
Declare #Table2 table (did int,Status varchar(50),name varchar(50),FID varchar(50))
Insert Into #Table2 values
(1,'Open.','Abu,def,xyz','233,344,455')
Select ID = Seq
,A.Status
,B.Name
,B.FID
From #Table2 A
Cross Apply (
Select Name=S1.RetVal
,FID =S2.RetVal
,Seq =S1.RetSeq
From [dbo].[udf-Str-Parse](A.Name,',') S1
Join [dbo].[udf-Str-Parse](A.FID,',') S2
on S1.RetSeq=S2.RetSeq
) B
Returns
ID Status Name FID
1 Open. Abu 233
2 Open. def 344
3 Open. xyz 455
The Split/Parse UDF if needed
CREATE FUNCTION [dbo].[udf-Str-Parse] (#String varchar(max),#Delimiter varchar(10))
Returns Table
As
Return (
Select RetSeq = Row_Number() over (Order By (Select null))
,RetVal = LTrim(RTrim(B.i.value('(./text())[1]', 'varchar(max)')))
From (Select x = Cast('<x>'+ replace((Select #String as [*] For XML Path('')),#Delimiter,'</x><x>')+'</x>' as xml).query('.')) as A
Cross Apply x.nodes('x') AS B(i)
);
--Select * from [dbo].[udf-Str-Parse]('Dog,Cat,House,Car',',')
--Select * from [dbo].[udf-Str-Parse]('John Cappelletti was here',' ')
--Select * from [dbo].[udf-Str-Parse]('this,is,<test>,for,< & >',',')

Resources