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

耗尽温柔 提交于 2019-11-28 23:49:27

问题


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:

  1. Might it be possible to append quotation marks w/ the SQL?

  2. Is there some T-SQL equivalent to the WITH CSV HEADER commands that are possible in other dialects?


回答1:


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.




回答2:


If you are using Visual Studio, Server Explorer is an alternative solution. You can correctly copy & paste the results from its grid.




回答3:


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"




回答4:


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



回答5:


column with line breaks: "mycolumn"

SELECT REPLACE(REPLACE(mycolumn, CHAR(13), ''), CHAR(10), '') as `mycolumn` FROM mytable

This replaces the linebreaks.




回答6:


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




回答7:


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



回答8:


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!




回答9:


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.



来源:https://stackoverflow.com/questions/2259446/need-to-export-fields-containing-linebreaks-as-a-csv-from-sql-server

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!