简体   繁体   中英

How to convert and use varchar data type as a datetime in mysql

Hello all, This is the format of my my-sql data type "rdate". Apr 1 2011 01:13:00:000PM

I want to use the order by rdate and i can't make it right order as the data type of rdate is varchar, So i want to convert it to date time , But no success. I am trying to use date_format(str_to_date( rdate , '%m/%d/%Y'), '%Y%m');

Thanks Mypixel

Try doing:

ORDER BY str_to_date(rdate,'%M %d %Y %h:%i:%s')

From the docs :

Your Date is in the Following format:

%M  Month name (January..December)
%d  Day of the month, numeric (00..31)
%Y  Year, numeric, four digits
...

You have to tell str_to_date the format that your string is in. This means the way the specific parts of the date are displayed, spaces, etc.

sqlfiddle demo

In your str_to_date function call, you need to specify what the format IS, not what you want it to be. Try this:

str_to_date(rdate, '%M %d %Y %h:%i:%s'));
UPDATE table SET rdate=str_to_date(rdate,'%M %d %Y %h:%i:%s')

只需将您的列转换为日期时间即可。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM