Solved

ms sql database mail and csv file attachments - how to "escape" correctly

Posted on 2013-05-27
2
2,196 Views
Last Modified: 2013-05-28
How do I ensure that query results placed in a csv file will "escape" correctly? Below works ok, but if there is a comma in the data, the csv file does not show correctly in excel. Is there some way to contain each cell's data in the csv so it will open correctly in excel?

Thanks.


DECLARE @sub VARCHAR(100)
DECLARE @qry VARCHAR(1000)
DECLARE @msg VARCHAR(250)
DECLARE @query NVARCHAR(1000)
DECLARE @query_attachment_filename NVARCHAR(520)

SELECT @sub = 'Nightly Report'
SELECT @msg = 'This is an automated message.'
SELECT @query = 'SELECT * FROM [sem].[dbo].[04AGSEMQuoteFormProduction] WHERE [sem].[dbo].[04AGSEMQuoteFormProduction].[ServerTime] >= cast(getdate() as date)'

SELECT @query_attachment_filename = 'report.csv'

EXEC msdb.dbo.sp_send_dbmail
            @profile_name = 'AG Reporting',
            @recipients = 'email@email.com',
            @body = @msg,
            @subject = @sub,
            @query = @query,
            @query_attachment_filename = @query_attachment_filename,
            @attach_query_result_as_file = 1,
            @query_result_header = 1,
            @query_result_width = 256 ,
            @query_result_separator = '	' ,
            @query_result_no_padding =1;

Open in new window

0
Comment
Question by:Mark B
2 Comments
 
LVL 24

Accepted Solution

by:
chaau earned 500 total points
Comment Utility
You have two options:
1. Remove all commas in the string values (using REPLACE(column1, ',', '')
2. Enclose each column in double quote ('"' + column1 + '"')

So, your query string will look like this:

SELECT @query = 'SELECT columnInt1, '"' + columnStr1 + '"', columnDate1, columnInt2, '"' + columnStr2 + '"', etc FROM [sem].[dbo].[04AGSEMQuoteFormProduction] WHERE [sem].[dbo].[04AGSEMQuoteFormProduction].[ServerTime] >= cast(getdate() as date)'

Open in new window


Read about CSV file rules in Wikipedia
0
 

Author Comment

by:Mark B
Comment Utility
Thanks, #2 worked.
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now