sql - MySQL date converstion -


i have table in mysql, have date , time column. format of column 01 february 2013 12:49:40. want extract date part , time part month part also. using month(01 february 2013 12:49:40) function it's throwing error. can't use substring, width of row not equal. should do.

first convert real date (what have text, mysql doesn't know it's date)

 select str_to_date(your_column, '%d %m %y %h:%i:%s') 

then can use functions year(), month(), time(), whatever want...

 select year(str_to_date(your_column, '%d %m %y %h:%i:%s')) your_table... 

even better of course, change column datatype datetime or timestamp. spare str_to_date() function every time.

do adding column datetime or timestamp table

alter table your_table add column your_column datetime; 

then "copy" column with

update yourtable set new_column = str_to_date(old_column, '%d %m %y %h:%i:%s'); 

then can drop current column.

alter table drop column your_current_column; 

or choose approach, several possibilities...


Comments

Popular posts from this blog

java - activate/deactivate sonar maven plugin by profile? -

python - TypeError: can only concatenate tuple (not "float") to tuple -

java - What is the difference between String. and String.this. ? -