sp_send_dbmail has formatting off on datetime when directing output to csv file

  • 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;

  • I sure hope someone can help me!

  • 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?

  • 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

  • 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.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

  • 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.

  • 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.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

  • Really as long as they customer gets the information, I don't think it would matter

Viewing 8 posts - 1 through 7 (of 7 total)

You must be logged in to reply to this topic. Login to reply