简体   繁体   中英

Convert varchar datatype to datetime in MySQL

How am I going to convert the time in my database which is in varchar(30) data type to datetime, I have this sample 11:45:24 09/23/2016 , suppose this have to be converted into 2016-9-23 11:45:24 . My column name is due_by . I have search and tried different suggestions but it seems that none of those queried successfully or correctly. I'm running it in MySQL workbench.

sample code

SELECT convert(varchar(30),'yyyy-mm-dd hh:mm:ss', 120) 
FROM csbrms.user_request;

在此处输入图片说明

Just want my due_by format to be equal to Now() column's format.

I think we cant directly convert that into now format as it is possible in Sql Server so I have tried this if you are ok with it, please have a look.

MySQL:

 SELECT 
CONCAT(STR_TO_DATE(
        RIGHT(TRIM(due_by), 10), '%m/%d/%Y'), ' ', 
        STR_TO_DATE(due_by,'%h:%i:%s')) nowFormat 
FROM csbrms.user_request;

SQL Server:

SELECT CONVERT(DATETIME, '11:45:24 09/23/2016', 120)

Use the following:

DECLARE @Date varchar(8)
set @Date='10102016'
SELECT CONVERT(datetime,RIGHT(@Date,4)+LEFT(@Date,2)+SUBSTRING(@Date,3,2))

OUTPUT:

-----------------------
2016-10-10 00:00:00.000

(1 row(s) affected)

Use the below for MySQL:

SELECT STR_TO_DATE('11:44:00 10/10/2016', '%h:%m:%s %m/%d/%Y') 
 STR_TO_DATE(str,format)

This is the inverse of the DATE_FORMAT() function. It takes a string str and a format string format. STR_TO_DATE() returns a DATETIME value if the format string contains both date and time parts, or a DATE or TIME value if the string contains only date or time parts. If the date, time, or datetime value extracted from str is illegal, STR_TO_DATE() returns NULL and produces a warning.

The server scans str attempting to match format to it. The format string can contain literal characters and format specifiers beginning with %. Literal characters in format must match literally in str. Format specifiers in format must match a date or time part in str. For the specifiers that can be used in format, see the DATE_FORMAT() function description.

mysql> SELECT STR_TO_DATE('01,5,2013','%d,%m,%Y');

        -> '2013-05-01'

mysql> SELECT STR_TO_DATE('May 1, 2013','%M %d,%Y');

        -> '2013-05-01'

Scanning starts at the beginning of str and fails if format is found not to match. Extra characters at the end of str are ignored.

mysql> SELECT STR_TO_DATE('a09:30:17','a%h:%i:%s');
        -> '09:30:17'
mysql> SELECT STR_TO_DATE('a09:30:17','%h:%i:%s');
        -> NULL
mysql> SELECT STR_TO_DATE('09:30:17a','%h:%i:%s');
        -> '09:30:17'

Unspecified date or time parts have a value of 0, so incompletely specified values in str produce a result with some or all parts set to 0:

mysql> SELECT STR_TO_DATE('abc','abc');

        -> '0000-00-00'

mysql> SELECT STR_TO_DATE('9','%m');

        -> '0000-09-00'

mysql> SELECT STR_TO_DATE('9','%s');

        -> '00:00:09'

Range checking on the parts of date values is as described in Section 11.3.1, “The DATE, DATETIME, and TIMESTAMP Types”. This means, for example, that “zero” dates or dates with part values of 0 are permitted unless the SQL mode is set to disallow such values.

mysql> SELECT STR_TO_DATE('00/00/0000', '%m/%d/%Y');

        -> '0000-00-00'

mysql> SELECT STR_TO_DATE('04/31/2004', '%m/%d/%Y');

        -> '2004-04-31'

If the NO_ZERO_DATE or NO_ZERO_IN_DATE SQL mode is enabled, zero dates or part of dates are disallowed. In that case, STR_TO_DATE() returns NULL and generates a warning.

Note :-

You cannot use format "%X%V" to convert a year-week string to a date because the combination of a year and week does not uniquely identify a year and month if the week crosses a month boundary. To convert a year-week to a date, you should also specify the weekday:

mysql> SELECT STR_TO_DATE('200442 Monday', '%X%V %W');

        -> '2004-10-18'

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