Convert varchar back into datetime??

  • Hello -

    I have a column in my View script that I convert to varchar(10) and want to convert it back to a date for the output to be in excel. Here is the original line

    CONVERT(VARCHAR(10), d.DocDate, 110) AS [Invoice Date]

    I have tried this line with no luck.

    CONVERT(VARCHAR(10), CONVERT(DATETIME, d.DocDate), 110) AS [Invoice Date]

    Can someone confirm if I have the right lines in the sql liine? Or I'm open to another way of writing it out.

    Thank you

    David

  • david.ostrander (4/24/2013)


    Hello -

    I have a column in my View script that I convert to varchar(10) and want to convert it back to a date for the output to be in excel. Here is the original line

    CONVERT(VARCHAR(10), d.DocDate, 110) AS [Invoice Date]

    I have tried this line with no luck.

    CONVERT(VARCHAR(10), CONVERT(DATETIME, d.DocDate), 110) AS [Invoice Date]

    Can someone confirm if I have the right lines in the sql liine? Or I'm open to another way of writing it out.

    Thank you

    David

    First, why not just drop the convert to varchar completely. If you are using it to drop the time portion, you will get that back converting back to a datetime value (just 00:00:00.000). If you need the later as a datetime then this works:

    dateadd(dd, datediff(dd, 0, d.DocDate), 0)

    If you still want to go through the convert to varchar, then this:

    CONVERT(DATETIME, CONVERT(VARCHAR(10), d.DocDate, 110)) AS [Invoice Date]

  • Lynn -

    Thank you for the tip.

    I used dateadd(dd, datediff(dd, 0, d.DocDate), 0) but how do I get rid of the

    "00:00:00:00"?

    Thanks,

    David

  • david.ostrander (4/24/2013)


    Lynn -

    Thank you for the tip.

    I used dateadd(dd, datediff(dd, 0, d.DocDate), 0) but how do I get rid of the

    "00:00:00:00"?

    Thanks,

    David

    If you want the datatype to be datetime, you don't.

    If you are exporting this data to Excel, let Excel deal with the formatting of the dates.

Viewing 4 posts - 1 through 3 (of 3 total)

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