Illegal XML Character - SSRS - sql-server

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

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

Convert string to datetime - SQL

I have a string parsed from XML which represents datetime.
String format is: '20200915114000' - (YYYYMMDDhhmmss)
Is there a function in SQL Server to convert or parse this string to datetime or should I split string manually and concatenate it into datetime?
How to do this, in 3 steps:
C:\> SQLCMD
1> select convert(datetime,'20200915114000' )
2> go
Msg 241, Level 16, State 1, Server ZES, Line 1
Conversion failed when converting date and/or time from character string.
1> select convert(datetime,'20200915 114000' )
2> go
Msg 242, Level 16, State 3, Server ZES, Line 1
The conversion of a varchar data type to a datetime data type resulted in an out-of-range value.
1> select convert(datetime,'20200915 11:40:00' )
2> go
-----------------------
2020-09-15 11:40:00.000
(1 rows affected)
1>
Conclusion you need to add a space and 3 ':' in the string to convert '20200915114000' to '20200915 11:40:00'. After this a simple CONVERT will do the trick.
Solution to my problem:
declare #sDate char(14), #sDateOK char(20)
set #sDate = '20200915114000'
set #sDateOK = substring(#sDate,1,8) + ' ' + substring(#sDate,9,2) + ':' + substring(#sDate,11,2) + ':' + substring(#sDate,13,2)
select convert(datetime,#sDateOK ) as Date

Why am i receiving Incorrect syntax near 'CAST' when assigning to an identifier

I am recieveing the following error
Msg 102, Level 15, State 1, Line 1
Incorrect syntax near 'CAST'.
Msg 156, Level 15, State 1, Line 1
Incorrect syntax near the keyword 'AS'.
Msg 156, Level 15, State 1, Line 1
Incorrect syntax near the keyword 'AS'.
When I execute the code below:
SET #BrandMarket = CONCAT(
'(CAST((SELECT COUNT (*) FROM [2018].[dbo].[USA19SoybeanTraitRawData] WHERE [2018].[dbo].[USA19SoybeanTraitRawData].[',
#BrandCol,
'] = ',
#CodeID,
') AS FLOAT)*100) / CAST((SELECT COUNT (*) FROM [2018].[dbo].[USA19SoybeanTraitRawData] WHERE [2018].[dbo].[USA19SoybeanTraitRawData].[',
#BrandCol,
'] IS NOT NULL) AS FLOAT)'
);
I the expect to execute the Select statement by:
INSERT #BrandMarketCodePer EXECUTE (#BrandMarket);
You should always provide the DBMS name to got a proper and faster response as each DBMS has their own version of SQL except the basic SQL commands. Going by your code, I guessed that your DBMS was probably SQL Server.
A CAST statement on its own is not an executable command. You have a missing SELECT. --> this was your reason of the error.
You do not need to provide the table name in your where clause when you have only one table used in your query. If you are using more than one table, you should try using an alias name for each table to make the code better readable. <-- this was not source of the error, but a recommendation.
I also removed a few unnecessary parenthesis and indented the code properly for better readability.
You can try this way (if your DBMS is SQL Server):
SET #BrandMarket = CONCAT('SELECT
CAST(
(
SELECT COUNT (*)
FROM [2018].[dbo].[USA19SoybeanTraitRawData]
WHERE [', #BrandCol, '] = ', #CodeID, '
) AS FLOAT
)*100
/
CAST(
(
SELECT COUNT (*)
FROM [2018].[dbo].[USA19SoybeanTraitRawData]
WHERE [', #BrandCol, '] IS NOT NULL
) AS FLOAT
)
'
);

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

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 .

Convert String to Date in T-SQL

I try to convert a string into a date in t-sql. However get results that I can't explain.
DECLARE #String as char(11)
DECLARE #TString as char(11)
SELECT #String = SUBSTRING([Flat File Source Error Output Column],1,CHARINDEX(',',[Flat File Source Error Output Column])-6)
FROM [ERROR].[Import_V2X]
SELECT #TString = '12/18/2009'
-- Check content before conversion
SELECT #TString as 'CheckTString'
SELECT #String as 'CheckString'
-- Convert the strings to date
SELECT CONVERT(date,#TString,101) as 'ConvertSuccess'
SELECT CONVERT(date,#String,101) as 'ConvertFails'
[Flat File Source Error Output Column] is defined as text in the table
This gives me the following result:
CheckTString
------------
12/18/2009
(1 row(s) affected)
CheckString
-----------
12/18/2009
(1 row(s) affected)
ConvertSuccess
--------------
2009-12-18
(1 row(s) affected)
ConvertFails
------------
Msg 241, Level 16, State 1, Line 16
Conversion failed when converting date and/or time from character string.
Anybody can explain me where the problem is or comes from ?
For me the strings look exactly the same :(
By the look of your output you have a line feed in the checkstring variable. If this is not just a copy and paste error in the question, that will cause the error that you are describing. See below
DECLARE #TString as char(11)
SELECT #TString = '
12/18/2009'
-- Check content before conversion
SELECT #TString as 'CheckTString'
-- Convert the strings to date
SELECT CONVERT(date,#TString,101) as 'ConvertFails'
Gives the following results.
(1 row(s) affected)
Msg 241, Level 16, State 1, Line 13
Conversion failed when converting date and/or time from character string.
If I had to guess it's because you're imported string has a non-visible character at the end of the string that doesn't allow it to convert. Your variable is char(11) but the string '12/18/2009' is only 10 characters long so that leaves room for 1 more character at the end.
Looks like #CheckString potentially has a newline character at the beginning.

Resources