I am exporting data from a SQL Server table to a .csv file, and then I use sp_send_email to email the file with data.
My problem is with this value:
Cantata Number 212 "Peasants Cantata", BWV 212
The value gets split into two columns in the .csv file that gets emailed. This value should be only in one column.
Some titles might contain a comma, which needs to be left in the string for those instances.
For example:
Cantata Number 212 Peasants Cantata" BWV 212"
I tried this method, but is not working:
Note: This SELECT statement resides inside a view vw_WeeklyReport
SELECT TOP 100 PERCENT
'"' + [p].[Title] + '"' [Title]
FROM
table
The code that exports the data and emails the .csv file:
BEGIN
SET NOCOUNT ON;
DECLARE @qry VARCHAR(8000);
-- Create the query, concatenating the column name as an alias
SET @Qry = 'SET NOCOUNT ON; SELECT Title FROM [vw_WeeklyReport] SET NOCOUNT OFF';
-- Send the e-mail with the query results in attachment.
EXEC [msdb].[dbo].[sp_send_dbmail]
@profile_name = 'default',
@recipients = '6lack@email.com',
@subject = 'Weekly Report',
@body = 'An attachment has been included in this email.',
@query_attachment_filename = 'WeeklyRep.csv',
@query = @qry,
@attach_query_result_as_file = 1,
@query_result_separator = ',',
@query_result_width = 32767,
@query_result_no_padding = 1;
END;