mysql - Convert text Date to DateTime? -


in sent field (varchar) text formatted this:

tue dec 04 16:10:05 gmt 2012

i convert datetime field type, solution in sql query?

generally speaking, 1 uses str_to_date() tasks of sort.

however, presence of timezone information in case presents problem str_to_date() cannot parse such strings. suggest first using substring_index() concat() extract data excluding timezone:

select str_to_date(          concat(            substring_index(sent, ' ',  4),            ' ',            substring_index(sent, ' ', -1)          ),          '%a %b %d %t %y'        )   my_table 

see on sqlfiddle.

to convert data type of existing column:

alter table my_table   add column sent_timestamp timestamp after sent ;  update my_table set    sent_timestamp = str_to_date(          concat(            substring_index(sent, ' ',  4),            ' ',            substring_index(sent, ' ', -1)          ),          '%a %b %d %t %y'        ) ;  alter table my_table   drop   sent,   change sent_timestamp sent timestamp ; 

Comments

Popular posts from this blog

java - Jmockit String final length method mocking Issue -

asp.net - Razor Page Hosted on IIS 6 Fails Every Morning -

c++ - wxwidget compiling on windows command prompt -