Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert text column to datetime in SQL

I have a column that's a text:

Remarks (text, null)

A sample value is

"5/21/2013 9:45:48 AM"

How do I convert it to a datetime format like this:

"2013-05-21 09:45:48.000"

The reason for the conversion is that I was trying to get the total number of hours between a datetime column and the date stamp in the Remarks column. I was thinking of something like this:

Remarks (text, null) - Date_Sent (datetime, null)

To be clear, the columns represent the datetime an inquiry by a customer was sent (Date_Sent) and the last response made by a representative regarding the inquiry (Response), so for a sample of a Date_Sent having a value of "2013-05-21 08:00:00.000" and a Response with a value of "5/21/2013 10:00:00 AM", I should get a value of 2.00 (2 hours). Unfortunately, in the database I'm working on, Remarks is a text and Date_Sent is a datetime.

like image 639
Poch Avatar asked May 21 '13 03:05

Poch


2 Answers

This works:

SELECT STR_TO_DATE(dateColumn, '%c/%e/%Y %r') FROM tabbleName WHERE 1
like image 84
Junu Avatar answered Sep 23 '22 16:09

Junu


In SQL Server , cast text as datetime

select cast('5/21/2013 9:45:48' as datetime)
like image 39
Mudassir Hasan Avatar answered Sep 20 '22 16:09

Mudassir Hasan