SQL Email to CSV, Results have Line Splitting issues

99封情书 提交于 2019-12-11 07:25:01

问题


This question is similar to several questions I've found where Line Feeds or carriage returns cause copying SQL grid results to Excel to have Line Splitting issues and your results will be cut off and started on the next row.

Name     Order#    Date        PartDescription     Store      Phone#
----     -------   ------      ----------------    -----      -----------
Tom's    3181      10/7/2017   SB4356 Artwork...   Downtown   208.452.6354

Will instead look like this

Name     Order#    Date        PartDescription     Store      Phone#
----     -------   ------      ----------------    -----      -----------
Tom's    3181      10/7/2017   SB4356 Artwork...   Downt   
own      2080.452.6354

However, this problem does not occur when I copy the SQL grid results to Excel. This problem occurs for me when I run the SQL as dynamic sql and email the results to myself via CSV. I'm not sure what the difference here is. The issue can be isolated to the Part Description field, its usually 100+ user-entered characters. If I remove that field this doesn't happen.

I've tried doing:

LTRIM(RTRIM(REPLACE(REPLACE(REPLACE(REPLACE( PartDescription, CHAR(13), ''), CHAR(10), ' '), CHAR(9), ' '), ',', ' ')))

and tried it in smaller steps and it hasn't had any effect. Like I said, it works copying the results to Excel. Just not when I email it to CSV. I'm not really sure how to isolate what inivisible charatcers might be causing this (if it is that).

I've used this method to fix others auto-reports in the past. This one is being stubborn. The SQL runs dynamically in SQL Server Agent, which then emails the results out in a csv format. I also tried saving the SQL in a view and called the view in dynamic SQL just for fun, no change.


回答1:


Had the same issue, turns out it was line wrapping when it hit 255 characters. I'm thinking you may have the same issue if your PartDescription field has many characters. Assuming you are using sp_send_dbmail, if you add the option:

@query_result_width=500

or whatever width you require, it should fix it.



来源:https://stackoverflow.com/questions/47168674/sql-email-to-csv-results-have-line-splitting-issues

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