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

后端 未结 9 547
野的像风
野的像风 2020-12-28 13:49

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 

Thi

相关标签:
9条回答
  • 2020-12-28 14:17

    column with line breaks: "mycolumn"

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

    This replaces the linebreaks.

    0 讨论(0)
  • 2020-12-28 14:20

    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.

    0 讨论(0)
  • 2020-12-28 14:21

    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.

    0 讨论(0)
提交回复
热议问题