Solved

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

Posted on 2013-05-27
2
2,257 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 25

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
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 the fundamental information of how to create a table.

733 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