SQL Server : Multiple rows XML - sql

I extracted xml file and move it SQL Server.
It now looked like this.
Now I'm trying to convert it to XML data type.
DECLARE #XML AS XML
SELECT #XML = convert(xml,[Column 0],2) FROM TestExtract
But Im getting this error
XML parsing: line 1, character 43, unable to switch the encoding
Maybe I should put all rows into one? then convert it?
Please advise for any options.
Thanks!

I used SSIS, flat file as a source and ole db(SQL server) as destination
Why you haven't imported the XML using XML Source instead of Flat File source?
If the XML file is well structured then you can use an XML Source to import data into SQL Server, there are many example found online:
Importing XML documents using SQL Server Integration Services
Extract Data by Using the XML Source
XML Source
Other approach
You can directly read the XML file from SQL Server using ad hoc queries (OPENROWSET):
Importing and Processing data from XML files into SQL Server tables
Current situation
First of all combine all rows into one value, then try to convert the value.
DECLARE #strXML VARCHAR(MAX)
DECLARE #XML AS XML
SET #strXML = ''
SELECT #strXML = #strXML + [Column 0] FROM Testextract
//SELECT #strXML = #strXML + [Column 0] + CHAR(13) + CHAR(10) FROM Testextract
SET #XML = convert(xml,#strXML,2)

Related

Can SSMS indent xml when pasting into Editor?

Does SSMS - SQL Server 2014 have an option to automatically indent XML text?
I save XML text in a column (nvarchar(max)) to analyze the input of an application.
Usually the result of my queries are set to grid and I copy and paste the result into the query editor to read it.
This is what I get:
<?xml version="1.0"?><farm-confirm source="orders.company.com"><Detail><item_keyid>3207890</item_keyid><item_code>50002035</item_code></Detail></farm-confirm>
This is what I would like:
<?xml version="1.0"?>
<farm-confirm source="orders.company.com">
<Detail>
<item_keyid>3207890</item_keyid>
<item_code>50002035</item_code>
</Detail>
</farm-confirm>
Thanks
Given the XML is well formed, the easies was to do this:
DECLARE #xml XML=N'Put your XML here';
SELECT #xml;
(Output to Grid-View)
And now just click on the XML. The XML-Viewer will present it formatted and indented.
Or take one of the free online XML prettyzizers.
Just google for online pretty xml formatter
update
If you get the XML (which is - in your case - a string actually) from a query, you might just wrap the column with CAST(MyColumn AS XML). This will offer you the XML-Viewer immediately...

Substring depending on Delimiter in the string

I am writing a procedure in order to handle a file name in SSIS.
Overview:
I am capturing the file name during a Text file load process in SSIS. I have written a procedure in order to split this file name into different components and return the values in form of Variables which I would be using further down the SSIS package.
Problem
This file name is of the format #FileName ="FILE_DATE_REF_DATETIME".All I need
to split this in a way like "FILE" , "DATE". I am able to achieve this by using
SUBSTRING(#Filename,0,CHARINDEX('_',#FileName))
and
Substring(#FileName,CHARINDEX('_',#FileName)+1,CHARINDEX('_',SUBSTRING(#Filename,CHARINDEX('_',#Filename)+1,Len(#Filename)))-1)
But here the major problem is when we get an additional '_' in FILE it completely goes wrong. Can anyone please suggest a way that I split the above file name format into FILE and DATE.
EDIT
Samples of FileNames:
asdfkg_20140710_ets20140710_0525_theds
asdjjf_they_20140710_ets20140710_0525_theds
oiuth_theyb_wgb_20140710_ets20140710_0526_theds
I need to extract anything before the 20140710 and also 20140710.
You can do it using PATINDEX instead of CHARINDEX
select SUBSTRING(#Filename,0,PATINDEX('%[_][0-9]%',#FileName))
If the last part of the filename is more reliable (no unexpected underscores), you could REVERSE it and use CHARINDEX to find the fourth underscore (and reverse the substrings again afterwards).
Otherwise, if you can trust the date format, you can use PATINDEX with a horrible expression like
'%[_][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][_]%'
Not the prettiest but it will work. LOL
DECLARE #FILE VARCHAR(55) = 'FILE_DATE_REF_DATETIME'
DECLARE #FILEFUN AS VARCHAR(55) = LEFT(#FILE,CHARINDEX('_',#FILE))
DECLARE #FILENAMEOUTPUT AS TABLE(Name Varchar(55))
WHILE LEN(#FILE) > 0
BEGIN
INSERT INTO #FILENAMEOUTPUT
SELECT REPLACE(#FILEFUN,'_','')
SET #FILE = REPLACE(#FILE,#FILEFUN,'')
SET #FILEFUN = iif(CHARINDEX('_',#FILE)=0,#FILE,LEFT(#FILE,CHARINDEX('_',#FILE)))
END
SELECT * FROM #FILENAMEOUTPUT

XML parsing: line 1, character 7, text/xmldecl not at the beginning of input

I am trying to insert a bit of xml in a xml column in my table, however I am getting an error
XML parsing: **line 1, character 7, text/xmldecl not at the beginning of input.
It worked fine in 2012 but its balking in 2008 R2, here's the XML I'm trying to insert... I have it down to its smallest size:
#xml = N'"<?xml version=\"1.0\" encoding=\"UTF-16\"?>"'
No clue why its different in 2008 R2. I have tried many different ways of representing that opening tag, but none have worked.
Including
#xml = N'"<?xml version="1.0" encoding="UTF-16"?>"'
Any help appreciated

escaping special characters in sql for generating xml file

I am using SQL server 2008 and I'm quite new to writing sql. My aim is to export data from a table into xml format to create a CAP xml file that can be used in our website. Currently, I'm just writing some select statements to retrieve data in the correct format. Here is the code:
select (SELECT TOP 5 [Master_Incident_Number] AS incidents
,[Jurisdiction] AS jurisdiction
,[Response_Date] AS Date
FROM [ESCAD_DW_System].[dbo].[CurrentIncidents_V] Incident
FOR XML PATH ('area'), type ) AS Alert for xml path (''),
ROOT ('?xml version = "1.0" encoding = "UTF-8"?')
However, I am getting 'invalid XML identifier' error for '?' symbol. Can anyone help?
You cannot use ROOT to add an xml encoding tag. The only way to do that is to convert the xml output into varchar(max) and prepend with your encoding tag. Keep in mind though that FOR XML output is UTF-16 by default, and therefore your UTF-8 encoding is probably unnecessary. Having said that, here is a simple example that uses a UDF to convert the xml into varchar(max):
create function gimmexml()
returns varchar(max)
as
begin
return (
select a='some', b='xml'
for xml path ('')
)
end
go
select '<?xml version="1.0" encoding="UTF-8"?>'+dbo.gimmexml();
Result:
<?xml version="1.0" encoding="UTF-8"?><a>some</a><b>xml</b>
Further reading:
http://www.devnewsgroups.net/group/microsoft.public.sqlserver.xml/topic60022.aspx
http://msdn.microsoft.com/en-us/library/ms345137%28v=sql.90%29.aspx

Need to export fields containing linebreaks as a CSV from SQL Server

I'm running a query from SQL Server Management Studio 2005 that has an HTML file stored as a a string, e.g.:
SELECT html
FROM table
This query displays in the "Results" window nicely, i.e., each row contains a string of the whole HTML file, with one record per row.
However, when I use the "Results to file" option, it exports it as an unusable CSV with line breaks in the CSV occurring wherever line breaks occurred in the field (i.e., in the HTML), rather than one per row as needed. I tried experimenting with the Query>Query Options for both the "Grid" and "Text" results, to no avail. The fields exported to the CSV do not appear to be enclosed within quotes.
Some ideas:
Might it be possible to append
quotation marks w/ the SQL?
Is there some T-SQL equivalent to the
WITH CSV HEADER commands that are
possible in other dialects?
I don't see where you will have much success exporting html to csv - it's really not what csv is meant for. You would be better off using an xml format, where the html code can be enclosed in a cdata element.
That said, you could try using the Replace function to remove the line breaks, and you could manually add the quotes - something like this:
select '"' + replace (replace (html, char(10), ''), char(13), '') + '"'
If your html value could have double quotes in it, you would need to escape those.
If you are using Visual Studio, Server Explorer is an alternative solution. You can correctly copy & paste the results from its grid.
According to the closest thing to a standard we have, a correctly formatted CSV-file should quote fields containing either the separator (most often ; or ,) or a linebreak.
SQL Server Management Studio can do that, but amazingly it doesn't have this option enabled by default. To enable, go to Tools → Options → Query Results → Results to Grid and check "Quote strings containing list separators when saving .csv results"
I know how old this is, but I found it and others might, as well. You might want to take Martijn van Hoof's answer one step further and remove possible tabs (char(9)) in addition to carriage return(13) and line feed(10).
SELECT REPLACE(REPLACE(REPLACE(mycolumn, CHAR(9), ''), CHAR(10), ''), CHAR(13), '') as 'mycolumn' FROM mytable
column with line breaks: "mycolumn"
SELECT REPLACE(REPLACE(mycolumn, CHAR(13), ''), CHAR(10), '') as `mycolumn` FROM mytable
This replaces the linebreaks.
i know this is very old question and has been answered now but this should help as well:
SELECT html
From table
For Xml Auto, Elements, Root('doc')
it should spit out an xml and then you can import that xml into excel
found this useful but I was still hitting problems as my field was type text so I cast the text as varchar(8000) and above replace works like a charm
REPLACE(REPLACE(CAST([TEXT FIELD] AS VARCHAR(8000)), CHAR(10), ' '), CHAR(13), ' ') AS 'Field Name',
SQL Server Import and Export Data tool, FTW!
Start -> Programs -> Microsoft SQL Server -> Import and Export Data
Sample query:
select *
from (
select 'Row 1' as [row], 'Commas, commas everywhere,' as [commas], 'line 1
line 2
line 3' as [linebreaks]
union all
select 'Row 2' as [row], 'Nor any drop to drink,' as [commas], 'line 4
line 5
line 6' as [data]
) a
CSV output:
"row","commas","linebreaks"
"Row 1","Commas, commas everywhere,","line 1
line 2
line 3"
"Row 2","Nor any drop to drink,","line 4
line 5
line 6"
Disclaimer: You may have to get creative with double-quotes in the data. Good luck!
I got around this limitation by creating an Access database, using the "Link to the data source by creating a linked table" feature, opening the "linked" table, then copy/paste to Excel from there. Excel can save the CSV data as needed. You should be able to connect directly from Excel too, but the "linked table" feature in Access let me set up several tables at once pretty quickly.