I want to removed duplicate words in a column of my SQL Server table. it will show error:
XML parsing: line 1, character 3114, semicolon expected
or
XML parsing: line 1, character 3045, semicolon expected
WITH Splitted AS
(
SELECT
Districtcd, ProspectId, Cat_Key_data,
CAST('<x>' + REPLACE(Cat_Key_data,' ','</x><x>') + '</x>' AS XML) AS TheParts
FROM
ES
WHERE
DistrictCd = 'PNE'
)
SELECT
Districtcd, ProspectId, Cat_Key_data,
TheParts.query('distinct-values(/x/text())').value('.','NVARCHAR(MAX)') AS Cat_Key_data_NEW
FROM
Splitted
WHERE
DistrictCd = 'PNE';
Related
I want to get matching record with all values in the variables.
e.g., 001, 3
Should return record that has 001 and 3 in their UnitId.
What is wrong with my statement? It only returns the matching record for the first item in comma delimited variable.
DECLARE #SearchTags AS VARCHAR(MAX) = '001, 3';
SELECT
Asset.UnitId, Asset.Description
FROM
Asset
WHERE
EXISTS (SELECT Split.XMLString.value('.', 'NVARCHAR(MAX)')
FROM
(SELECT CAST('<X>'+REPLACE(#SearchTags, ',', '</X><X>')+'</X>' AS XML) AS [XML]) AS XMLString
CROSS APPLY
[XML].nodes('/X') AS Split(XMLString)
WHERE
Asset.UnitId LIKE '%' + Split.XMLString.value('.', 'NVARCHAR(MAX)') + '%'
)
SQL Fiddle
I am using the following to spilt comma separated string into columns (SQL Server 2014):
function [dbo].[splitString](#input Varchar(max), #Splitter Varchar(99)) returns table as
Return
SELECT Split.a.value('.', 'NVARCHAR(max)') AS Data FROM
( SELECT CAST ('<M>' + REPLACE(#input, #Splitter, '</M><M>') + '</M>' AS XML) AS Data
) AS A CROSS APPLY Data.nodes ('/M') AS Split(a);
When I try to split the following:
Front Office,Food & Beverage,Housekeeping,Human Resource & Training,Reservation,Other
I get the following error: XML parsing: line 1, character 82, illegal name character
Is there a way to include special characters in my function?
Please try the following solution.
Notable points:
CData section protects against XML entities like ampersand and the
like.
text() inside .nodes() method is for performance reasons.
TRY_CAST() will return NULL, but will not error out.
SQL
DECLARE #input Varchar(max) = 'Front Office,Food & Beverage,Housekeeping,Human Resource & Training,Reservation,Other'
, #Splitter Varchar(99) = ',';
SELECT Split.a.value('.', 'NVARCHAR(max)') AS Data
FROM ( SELECT TRY_CAST('<M><![CDATA[' + REPLACE(#input, #Splitter, ']]></M><M><![CDATA[') + ']]></M>' AS XML) AS Data
) AS A CROSS APPLY Data.nodes ('/M/text()') AS Split(a);
Msg 9420, Level 16, State 1, Line 1
XML parsing: line 931, character 10, illegal xml character
Is there a way to eradicate the illegal xml characters, as I keep getting thrown this error when I run the code below?
SELECT
ReportPath = C.Path
,CAST(CAST(C.content AS VARBINARY(MAX)) AS XML) AS reportXML
FROM ReportServer.dbo.Catalog AS C
WHERE C.Type = 2
I am trying this query
SELECT
[Code],
tbl.Id AS PnlId,
CAST('<t>' + REPLACE(tbl.Line,
'|',
'</t><t>') + '</t>' AS xml) x
FROM
[dbo].[Temp] AS tbl
As example :
Id Line
1 102|103|104 // suppose line is too long
I always got
XML parsing: line 64, character 74, unexpected end of input
Any idea how to resolve it ? Many thanks in advance .
I have a data flow with over 150 columns and many of them are of data type string, I need to remove comma's and double quotes from the value of every text column because they are causing issues when I export the data to CSV, is there an easy way to do that other than doing it explicitly for every column in a derived column or script compnent?
In the script generator below, put all your column names from the CSV in the order that you want, and run it.
;With ColumnList as
(
Select 1 Id, 'FirstColumn' as ColumnName
Union Select 2, 'SecondColumn'
Union Select 3, 'ThirdColumn'
Union Select 4, 'FourthColumn'
Union Select 5, 'FifthColumn'
)
Select 'Trim (Replace (Replace (' + ColumnName + ', '','', ''''), ''"'', ''''))'
From ColumnList
Order BY Id
The Id column should contain a proper sequence (I would generate that in EXCEL. Here is the output
---------------------------------------------------------
Trim (Replace (Replace (FirstColumn, ',', ''), '"', ''))
Trim (Replace (Replace (SecondColumn, ',', ''), '"', ''))
Trim (Replace (Replace (ThirdColumn, ',', ''), '"', ''))
Trim (Replace (Replace (FourthColumn, ',', ''), '"', ''))
Trim (Replace (Replace (FifthColumn, ',', ''), '"', ''))
(5 row(s) affected)
You could just cut and paste from here into your SSIS dataflow.
I found a way to loop through columns in a script component, then i was able to check the column data type and do a replace function, here is the post i used.