Link to home
Start Free TrialLog in
Avatar of Fred Webb
Fred WebbFlag for United States of America

asked on

DB Mail Export to Excel some row wrap to new row

I have a DB Mail script to export to excel as a CSV but it is wrapping some text to new new row. I am using a view to a Linked Server to generate the Dynamic SQL and the view which uses "REPLACE(REPLACE(CAST(rma.problem_description AS VARCHAR(MAX)),CHAR(10), ''), CHAR(13), '')AS problem_description" to remove the CR LF but it still wraps. if I run the VIEW in a query window copy with headers and past into Excel it does not wrap.

View
SET QUOTED_IDENTIFIER ON
GO

ALTER VIEW [dbo].[SSG_RA_DATA] 
AS 
                 SELECT      CAST(rma.created_on AS DATE) ra_created,  rma.return_number, model_group.name AS model_no, 
(CASE WHEN rma.item_number IS NULL THEN 'None Avaliable' ELSE rma.item_number END ) AS item_number, 
(CASE WHEN rma.serial_number IS NULL THEN 'Non-Serialized' ELSE rma.serial_number END) AS serial_number ,  
REPLACE(REPLACE(CAST(rma.problem_description AS VARCHAR(MAX)),CHAR(10), ''), CHAR(13), '')AS problem_description
FROM            [AMCHARWEB2\WEBSQL2].returnauth.[dbo].[rma] INNER JOIN
[AMCHARWEB2\WEBSQL2].returnauth.[dbo].[model_group] ON rma.model_group_id = model_group.id
WHERE  CAST(rma.created_on AS DATE)>= '2016-01-01' AND CAST(rma.created_on AS DATE)<= GETDATE()+1
AND (CASE WHEN model_group.name = 'DEFAULT' AND rma.item_number IS NULL OR rma.item_number ='' THEN 1 ELSE '' END <>1)   

GO

Open in new window


DB MAIL
DECLARE
    @tab char(1) = CHAR(9)

EXEC msdb.dbo.sp_send_dbmail
    @profile_name = 'Tri Company DB Mail',
    @recipients = 'name@email.com',
    @query = 'SET NOCOUNT ON 
              SELECT        ra_created, return_number, model_no, item_number, serial_number, problem_description
FROM          ATI.[dbo].[SSG_RA_DATA]' ,
    @subject = 'RA results',
    @attach_query_result_as_file = 1,
    @query_attachment_filename='RATEST.CSV',
    @query_result_separator=@tab,
    @query_result_no_padding=1

Open in new window

Avatar of ste5an
ste5an
Flag of Germany image

The problem is not SQL Server, it's Excel and the CSV format. You have a line break as content in one cell. And Excel is not good at handling those.

Possible fixes:
1) strip line breaks in the query using REPLACE().
2) read the file by using the Excel external data import.
Avatar of Fred Webb

ASKER

I don't believe the issue is Excel, as I stated in my original post if I run the VIEW in a query window then copy with headers and past into Excel it does not wrap.
I tried exporting as xlsx but when opening the file in Excel it indicates that the file format or extension is invalid.

1) I did use REPLACE in the view used by the dynamic SQL "REPLACE(REPLACE(CAST(rma.problem_description AS VARCHAR(MAX)),CHAR(10), ''), CHAR(13), '')AS problem_description" to remove the CR LF

2) This needs to be automated hence the use of DB mail
suggest you attach to post example of the csv file which is doing it - have you examined that to see where it is splitting?
I cant attach an example due to the sensitivity of the data. The weird thing is that if I run the view in a query window, copy with headers and paste it into the spread sheet it does not wrap those rows, it only does that when I send it as a DBmail .CSV attachment
Craft a repro showing that behavior.

btw, this can already help you to understand the problem yourself.
is there anything in Excel that will let me see formatting like in word
Don't use Excel. Use a text editor like Notepad++ or sublime.
Ok, I pasted the CSV content into Note++ and there is a CRLF  where it wraps even though I used the replace function.
What column? Seems that your REPLACE() does not cover this.
The problem_description column I use the replace function in the View
REPLACE(REPLACE(CAST(rma.problem_description AS VARCHAR(MAX)),CHAR(10), ''), CHAR(13), '')AS problem_description to remove the CR LF but it still wraps.

and I have even used it in the dynamic SQL
DECLARE
    @tab char(1) = CHAR(9)

EXEC msdb.dbo.sp_send_dbmail
    @profile_name = 'Tri Company DB Mail',
    @recipients = 'xxxx@xxxxx.com',
    @query = 'SET NOCOUNT ON
              SELECT        ra_created, return_number, model_no, item_number, serial_number, REPLACE(REPLACE(CAST(problem_description AS VARCHAR(MAX)),CHAR(10), ''''), CHAR(13), '''')AS problem_description
FROM          ATI.[dbo].[SSG_RA_DATA]' ,
    @subject = 'RA results',
    @attach_query_result_as_file = 1,
    @query_attachment_filename='RATEST.CSV',
    @query_result_separator=@tab,
    @query_result_no_padding=1

I copied one of the rows that wrap from Excel and pasted it into Notepad++ and as you can see there are CRLF codes, so it is not stripping them out.
User generated image
Please try a simpler sample like:

DECLARE @Sample TABLE
    (
      ID INT ,
      ContentText VARCHAR(255)
    );

INSERT  INTO @Sample
VALUES  ( 1, 'Some Text.' ),
        ( 2, 'Some more 
Text' );

SELECT  * ,
        REPLACE(S.ContentText, CHAR(13) + CHAR(10), '') AS SanitizedContentText
FROM    @Sample S;

Open in new window

Nope... Still didn't work.  If I run the VIEW in a query window then copy with headers and past into Excel it does not wrap.
OK... so I ran the Dynamic SQL as a standalone copied with headers and pasted into Excel it does not wrap.
DECLARE   @query NVARCHAR (4000)
SET  @query = 'SET NOCOUNT ON
 SELECT        ra_created, return_number, model_no, item_number, serial_number, REPLACE(problem_description, CHAR(13) + CHAR(10), '''') AS problem_description
FROM          ATI.[dbo].[SSG_RA_DATA]'
      EXECUTE sp_executesql @query
So... I would guess it is how SQL is exporting it to the CSV file
SOLUTION
Avatar of ste5an
ste5an
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
2008R2
Have you tested my short sample?
The thing is that not every line wraps
hmm, try adding an explicit HTML body as in my sample.
is it actually a CR followed by LF or could it just be CR or just LF.  In your earlier examples you were replacing CR then LF with blank but above only specifically CR LF.

How about something like this as part of what you have above to replace and highlight where there are CR or LF in there

Steve

21:        REPLACE(REPLACE(S.ContentText, CHAR(13) , '*CR*'),CHAR(10) , '*LF*') AS SanitizedContentText
Steve,
Added your suggestion and it still wraps the same rows. The weird thing is if I just run the query and copy with headers then Paste into Excel no problem, it is only with the DB Mail export to excel that it wraps
So does it show any *CR* or *LF* in the text though which is what I was suggesting replacing with?
hmm, the only thing I can imagine, is that for some reason your e-mail gets an artificial line break, cause there was once on defined (72 chars per line or so)..
Steve, No...
Odd that it doesn't always do it.  Is it at certain character width etc. i.e. do you need to use for your server:

@query_result_width = 1000

or something

Steve
Steve...  I was looking for the  *CR**LF*  in the rows that were wrapping in the export from the DB mail but they did show up in other rows that weren't wrapping.
Ok so CR LF Isn't in your data then, they ARE being replaced properly with blanks normally (or *CR**LF* above) so it is added elsewhere.  Possibly result-width?
OK... where would I increase the results width
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Steve I am Splitting the Points as ste5an has been helping from the beginning and I used his format to clean up my code.
Gather that worked then!

Fine by me, glad it helped.  Had missed that not being there earlier as was concentrating / assuming the CR/LF was coming from the data as you had until we proved it wasn't.

Steve