Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

sp_send_dbmail has formatting off on datetime when directing output to csv file Expand / Collapse
Author
Message
Posted Monday, March 31, 2014 2:55 PM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Monday, July 21, 2014 12:39 PM
Points: 229, Visits: 803
I am running this sql which creates a csv file and sends the output via email. One of the columns in the output. is a datetime field. When it is formatted in the csv file, a portion of the datetime column is missing when using the tab separator. I changed the separator to be a comma and it no longer loses the data in the datetime field. It formats correctly. Now the issue with the comma as the separator causes the file when you open it in excel to show the commas and each row is treated as one field. However, if I view the file instead of opening it the format is correct. I'm just not sure how to make the file look correct (without seeing the commas) if the user opens the attachment in email,


DECLARE @tab VARCHAR(1)
SET @tab = CHAR(9)
DECLARE @sub VARCHAR(100)
DECLARE @qry VARCHAR(1000)
DECLARE @msg VARCHAR(250)
DECLARE @query NVARCHAR(1000)
DECLARE @query_attachment_filename NVARCHAR(520)

SELECT @sub = 'CxDB NYTimes Report'
SELECT @msg = 'Please refer to the attached spread sheet for the report.'
SELECT @query = ' SET NOCOUNT ON;
select * from cxdb.dbo.NewYorkTimesView '

SELECT @query_attachment_filename = 'CxDB NYTimes Report.xls'

EXEC msdb.dbo.sp_send_dbmail
@profile_name = 'ms sql dba',
@recipients = 'patti.hall.johnson@nytimes.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 = 32767,
@query_result_separator = @tab,
@query_no_truncate = 0,
@exclude_query_output = 0,
@query_result_no_padding =1;

Post #1556725
Posted Tuesday, April 1, 2014 6:44 AM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Monday, July 21, 2014 12:39 PM
Points: 229, Visits: 803
I sure hope someone can help me!
Post #1556946
Posted Tuesday, April 1, 2014 3:03 PM
SSC Eights!

SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!

Group: General Forum Members
Last Login: Monday, July 21, 2014 7:44 PM
Points: 817, Visits: 1,646
Did you try changing your query to get the columns instead of using SELECT *? Probably a good idea in general, since it means you won't risk having columns which you may not want to have, show up in the result set.

If you do it that way, you could also potentially try using CONVERT to change the DATETIME column into a VARCHAR column, formatted the way you want it. That might solve your problems?
Post #1557221
Posted Tuesday, April 1, 2014 3:26 PM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Monday, July 21, 2014 12:39 PM
Points: 229, Visits: 803
thank you. I was attempting to do select of individual columns and put it down. I will continue with that again and let you know
Post #1557238
Posted Tuesday, April 1, 2014 4:26 PM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Today @ 11:31 AM
Points: 36,728, Visits: 31,180
I think your biggest problem is that you're using a .xls extension on the file. It's NOT an Excel file. Also, if someone opens even a text file in Excel, Excel has its own idea of how to format columns... especially dates.

--Jeff Moden
"RBAR is pronounced "ree-bar" and is a "Modenism" for "Row-By-Agonizing-Row".

First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column."

(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013

Helpful Links:
How to post code problems
How to post performance problems
Post #1557265
Posted Tuesday, April 1, 2014 9:29 PM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Monday, July 21, 2014 12:39 PM
Points: 229, Visits: 803
Hi, I didnt realize that I sent the script with an xls extension. I had changed it to a csv file and it still didn't work. When I did have it as an xls file it didn't work when I attempted to open the attached file.

Post #1557304
Posted Tuesday, April 1, 2014 10:58 PM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Today @ 11:31 AM
Points: 36,728, Visits: 31,180
Does it have to be an attached file or can it just be a nicely formatted email message that someone could copy and paste into Excel?

--Jeff Moden
"RBAR is pronounced "ree-bar" and is a "Modenism" for "Row-By-Agonizing-Row".

First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column."

(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013

Helpful Links:
How to post code problems
How to post performance problems
Post #1557310
Posted Wednesday, April 2, 2014 6:51 AM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Monday, July 21, 2014 12:39 PM
Points: 229, Visits: 803
Really as long as they customer gets the information, I don't think it would matter
Post #1557453
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse