SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Convert varchar back into datetime??


Convert varchar back into datetime??

Author
Message
david.ostrander
david.ostrander
SSC-Addicted
SSC-Addicted (400 reputation)SSC-Addicted (400 reputation)SSC-Addicted (400 reputation)SSC-Addicted (400 reputation)SSC-Addicted (400 reputation)SSC-Addicted (400 reputation)SSC-Addicted (400 reputation)SSC-Addicted (400 reputation)

Group: General Forum Members
Points: 400 Visits: 692
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
Lynn Pettis
Lynn Pettis
SSC Guru
SSC Guru (93K reputation)SSC Guru (93K reputation)SSC Guru (93K reputation)SSC Guru (93K reputation)SSC Guru (93K reputation)SSC Guru (93K reputation)SSC Guru (93K reputation)SSC Guru (93K reputation)

Group: General Forum Members
Points: 93505 Visits: 38955
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]



Cool
Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
david.ostrander
david.ostrander
SSC-Addicted
SSC-Addicted (400 reputation)SSC-Addicted (400 reputation)SSC-Addicted (400 reputation)SSC-Addicted (400 reputation)SSC-Addicted (400 reputation)SSC-Addicted (400 reputation)SSC-Addicted (400 reputation)SSC-Addicted (400 reputation)

Group: General Forum Members
Points: 400 Visits: 692
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
Lynn Pettis
Lynn Pettis
SSC Guru
SSC Guru (93K reputation)SSC Guru (93K reputation)SSC Guru (93K reputation)SSC Guru (93K reputation)SSC Guru (93K reputation)SSC Guru (93K reputation)SSC Guru (93K reputation)SSC Guru (93K reputation)

Group: General Forum Members
Points: 93505 Visits: 38955
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.

Cool
Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search