XML parsing: line 64, character 74, unexpected end of input - sql-server

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 .

Related

XML parsing: line 1, character 3114, semicolon expected

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';

Illegal XML Character - SSRS

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

How to replace right most string in SQL

I want to replace rightmost same characters from string.
e.g string is like in this format
"GGENXG00126""XLOXXXXX"
in sql but last consecutive X lenght is not fix.
I already Tried in SQL is
select REPLACE('"GGENXG00126""XLOXXXXX"', 'X', '')
but using this all 'X' is removed. I want to remove only rightmost same characters and output expected "GGENG00126""LO".
You can replace all x with spaces, RTRIM, then undo the replacements:
SELECT '"' + REPLACE(RTRIM(REPLACE(SUBSTRING(str, 2, LEN(str) - 2), 'X', ' ')), ' ', 'X') + '"'
FROM (VALUES
('"GGENXG00126""XLOXXXXX"')
) v(str)
-- "GGENXG00126""XLO"
An alternative idea using PATINDEX and REVERSE, to find the first character that isn't the final character in the string. (Assumes all strings are quoted):
SELECT REVERSE(STUFF(R.ReverseString,1,PATINDEX('%[^' + LEFT(R.ReverseString,1) + ']%',R.ReverseString)-1,'')) + '"'
FROM (VALUES('"GGENXG00126""XLOXXXXX"'))V(YourString)
CROSS APPLY (VALUES(STUFF(REVERSE(V.YourString),1,1,''))) R(ReverseString);
You can try this below option-
DECLARE #InputString VARCHAR(200) = 'GGENXG00126""XLOXXXXX'
SELECT
LEFT(
#InputString,
LEN(#InputString)+
1-
PATINDEX(
'%[^X]%',
REVERSE(#InputString)
)
)
Output is-
GGENXG00126""XLO

How do I change ;A;B;C; to ('A', 'B', 'C')?

I need to convert a value list separated by semi-colons, including in front, into a regular value list with quotes and commas. There might only be one value, or there may be many values in the field.
I thought about replacing the ; with a comma, but then I still have a comma in front and behind, and I also need to add single quotes.
REPLACE(S_List, ';', ',')
I want ;a;b;c; to be 'a','b','c' or at least a,b,c but I don't know what to do with the beginning and end semicolons
With substring() and replace():
declare #slist varchar(100) = ';a;b;c;'
select substring(replace(#slist, ';', ''','''), 3, len(replace(#slist, ';', ''',''')) - 4)
See the demo.
Result:
'a','b','c'
Edit.
Use it like this in your table:
select
case when s_list like '%;%;%' then
substring(replace(s_list, ';', ''','''), 3, len(replace(s_list, ';', ''',''')) - 4)
else s_list
end
from tablename
See the demo.
You could try:
SELECT REPLACE(LEFT(S_List,1),';','''')
SELECT REPLACE(RIGHT(S_List,1),';','''')
SELECT REPLACE(S_List,';', ''',''')
If your values do not have spaces, you can use a trim() trick:
select '''' + replace(ltrim(rtrim(replace(s_list, ',', ' '))), ',', ''',''') + ''''

How use money format in pivot in SQL Server

I am using this code. It returns the sum() of price and shows it with delimiter like money format
SELECT
REPLACE(CONVERT(VARCHAR(20), CAST( sum(unitprice) AS MONEY), 1), '.00', '')
FROM
tchargecard_factor
GROUP BY
datetimeentry
and it works fine.
Please tell me how can I get this result from pivot table?
My code is :
'SELECT *, (' + #GrandTotalCol + ') AS [Grand Total]
INTO #temp_MatchesTotal
FROM
(SELECT
idpoint, [last mendub name], pointsalename,
agentname, mobile, remain, typegroupname
FROM
zomorod_webapp_temp.dbo.chargecard_sum_typegroup4) A
PIVOT
(SUM(remain)
FOR typegroupname IN (' +#columnHeaders + ')) B
ORDER BY
idpoint
When I use this code, it is not allowing me to use cast or convert type in sum(remain). I get this error:
Msg 195, Level 15, State 1, Line 9
'REPLACE' is not a recognized aggregate function
How can I fix it?

Resources