Solved

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

Posted on 2013-05-27
2
2,208 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
ID: 39199930
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
ID: 39200376
Thanks, #2 worked.
0

Featured Post

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

948 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

22 Experts available now in Live!

Get 1:1 Help Now