I have a test table in my database with the following structure: id(PK, Auto_Increment), dataI(datetime), dataF (datetime).
Using MySQL Workbench, when I try to insert a record I am having problems. I tried different ways using str_to_date to fix it.
INSERT INTO test(dataI, dataF)
VALUES(STR_TO_DATE('2020-01-10 18:00:00', '%YYYY-%MM-%DD hh:mm:ss'), STR_TO_DATE('2020-01-10 24:00:00', '%YYYY-%MM-%DD hh:mm:ss'))
Error code 1411: Incorrect datetime value for function str_to_date
'24:00:00' is not a valid time component for a DATETIME literal.
The format specification must match the string. The list of valid specifiers is documented under the DATE_FORMAT function, which is referenced under the documentation for the STR_TO_DATE function on the same page.
Reference:
https://dev.mysql.com/doc/refman/8.0/en/date-and-time-functions.html#function_date-format
Format of that first value could be specified as '%Y-%m-%d %h %H:%i:%s'
or more concisely as '%Y-%m-%d %T'
We don't need to use STR_TO_DATE function when date literals are supplied in the default format; MySQL will do the conversion to DATETIME implicitly.
We could do this:
... VALUES ('2020-01-10 18:00:00','2020-01-11 00:00:00')
Or, if we want to use the STR_TO_DATE, we specify a format that matches our string
... VALUES( STR_TO_DATE('2020-01-10 18:00:00','%Y-%m-%d %T')
, STR_TO_DATE('2020-01-11 00:00:00','%Y-%m-%d %T')
)
^^^^^^^^^^^^
Your format strings are wrong. For example, %YYYY should just be %Y; %YYYY means a 4-digit year followed by the literal string YYY.
The format strings should be %Y-%m-%d %H:%i:%s.
See the complete list of format directives here.
Related
I have a string column which acts as a date and I want to select it as a DATE.
Is it possible?
My sample data format would be:
month/day/year -> 12/31/2011
As was told at MySQL Using a string column with date text as a date field, you can do
SELECT STR_TO_DATE(yourdatefield, '%m/%d/%Y')
FROM yourtable
You can also handle these date strings in WHERE clauses. For example
SELECT whatever
FROM yourtable
WHERE STR_TO_DATE(yourdatefield, '%m/%d/%Y') > CURDATE() - INTERVAL 7 DAY
You can handle all kinds of date/time layouts this way. Please refer to the format specifiers for the DATE_FORMAT() function to see what you can put into the second parameter of STR_TO_DATE().
STR_TO_DATE('12/31/2011', '%m/%d/%Y')
Here's another two examples.
To output the day, month, and year, you can use:
select STR_TO_DATE('14/02/2015', '%d/%m/%Y');
Which produces:
2015-02-14
To also output the time, you can use:
select STR_TO_DATE('14/02/2017 23:38:12', '%d/%m/%Y %T');
Which produces:
2017-02-14 23:38:12
http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html
use the above page to refer more Functions in MySQL
SELECT STR_TO_DATE(StringColumn, '%d-%b-%y')
FROM table
say for example use the below query to get output
SELECT STR_TO_DATE('23-feb-14', '%d-%b-%y') FROM table
For String format use the below link
http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_date-format
The following illustrates the syntax of the STR_TO_DATE() function:
STR_TO_DATE(str,fmt);
The STR_TO_DATE() converts the str string into a date value based on the fmt format string. The STR_TO_DATE() function may return a DATE , TIME, or DATETIME value based on the input and format strings. If the input string is illegal, the STR_TO_DATE() function returns NULL.
The following statement converts a string into a DATE value.
SELECT STR_TO_DATE('21,5,2013','%d,%m,%Y');
Based on the format string ‘%d, %m, %Y’, the STR_TO_DATE() function scans the ‘21,5,2013’ input string.
First, it attempts to find a match for the %d format specifier, which
is a day of the month (01…31), in the input string. Because the
number 21 matches with the %d specifier, the function takes 21 as the
day value.
Second, because the comma (,) literal character in the format string
matches with the comma in the input string, the function continues to
check the second format specifier %m , which is a month (01…12), and
finds that the number 5 matches with the %m format specifier. It
takes the number 5 as the month value.
Third, after matching the second comma (,), the STR_TO_DATE()
function keeps finding a match for the third format specifier %Y ,
which is four-digit year e.g., 2012,2013, etc., and it takes the
number 2013 as the year value.
The STR_TO_DATE() function ignores extra characters at the end of the input string when it parses the input string based on the format string. See the following example:
SELECT STR_TO_DATE('21,5,2013 extra characters','%d,%m,%Y');
More Details : Reference
I have a string column which acts as a date and I want to select it as a DATE.
Is it possible?
My sample data format would be:
month/day/year -> 12/31/2011
As was told at MySQL Using a string column with date text as a date field, you can do
SELECT STR_TO_DATE(yourdatefield, '%m/%d/%Y')
FROM yourtable
You can also handle these date strings in WHERE clauses. For example
SELECT whatever
FROM yourtable
WHERE STR_TO_DATE(yourdatefield, '%m/%d/%Y') > CURDATE() - INTERVAL 7 DAY
You can handle all kinds of date/time layouts this way. Please refer to the format specifiers for the DATE_FORMAT() function to see what you can put into the second parameter of STR_TO_DATE().
STR_TO_DATE('12/31/2011', '%m/%d/%Y')
Here's another two examples.
To output the day, month, and year, you can use:
select STR_TO_DATE('14/02/2015', '%d/%m/%Y');
Which produces:
2015-02-14
To also output the time, you can use:
select STR_TO_DATE('14/02/2017 23:38:12', '%d/%m/%Y %T');
Which produces:
2017-02-14 23:38:12
http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html
use the above page to refer more Functions in MySQL
SELECT STR_TO_DATE(StringColumn, '%d-%b-%y')
FROM table
say for example use the below query to get output
SELECT STR_TO_DATE('23-feb-14', '%d-%b-%y') FROM table
For String format use the below link
http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_date-format
The following illustrates the syntax of the STR_TO_DATE() function:
STR_TO_DATE(str,fmt);
The STR_TO_DATE() converts the str string into a date value based on the fmt format string. The STR_TO_DATE() function may return a DATE , TIME, or DATETIME value based on the input and format strings. If the input string is illegal, the STR_TO_DATE() function returns NULL.
The following statement converts a string into a DATE value.
SELECT STR_TO_DATE('21,5,2013','%d,%m,%Y');
Based on the format string ‘%d, %m, %Y’, the STR_TO_DATE() function scans the ‘21,5,2013’ input string.
First, it attempts to find a match for the %d format specifier, which
is a day of the month (01…31), in the input string. Because the
number 21 matches with the %d specifier, the function takes 21 as the
day value.
Second, because the comma (,) literal character in the format string
matches with the comma in the input string, the function continues to
check the second format specifier %m , which is a month (01…12), and
finds that the number 5 matches with the %m format specifier. It
takes the number 5 as the month value.
Third, after matching the second comma (,), the STR_TO_DATE()
function keeps finding a match for the third format specifier %Y ,
which is four-digit year e.g., 2012,2013, etc., and it takes the
number 2013 as the year value.
The STR_TO_DATE() function ignores extra characters at the end of the input string when it parses the input string based on the format string. See the following example:
SELECT STR_TO_DATE('21,5,2013 extra characters','%d,%m,%Y');
More Details : Reference
I have a table with type DATE. How do i convert the below date to be able to insert it into the table.
15-JUL-12 3:09pm
I tried the following, but it keeps saying Incorrect date time values.
STR_TO_DATE('15-JUL-12 3:09pm', '%d-%m-%y %h:%i%p')
STR_TO_DATE() is the correct function to use, but there is a problem with the format string.
Use %b for abbreviated month name (or generally %M for month names) in your format string. E.g.:
STR_TO_DATE('15-JUL-12 3:09pm', '%d-%b-%y %h:%i%p')
See:
http://dev.mysql.com/doc/refman/5.6/en/date-and-time-functions.html#function_date-format
for documentation of format symbols
select DATE_FORMAT('8:48:30 AM', '%H:%i:%s')
It returns Null why ?
but when using
select DATE_FORMAT(CURTIME(), '%H:%i:%s')
It return formatted value.
It's returning NULL because MySQL isn't successfully parsing the string into a valid DATETIME value.
To fix the problem, use the STR_TO_DATE function to parse the string into a TIME value,
SELECT STR_TO_DATE('8:48:30 AM', '%h:%i:%s %p')
Then, to get the TIME value converted to a string in a particular format, use the TIME_FORMAT function, e.g. 24-hour clock representation:
SELECT TIME_FORMAT(STR_TO_DATE( '8:48:30 AM', '%h:%i:%s %p'),'%H:%i:%s')
returns:
--------
08:48:30
The method DATE_FORMAT is used to display date and time, however in the first you are not assigning any date except time, so its is throwing null.
From the manuals -
DATE_FORMAT Formats the date value according to the format string.
In MySql version 5.5 SELECT DATE_FORMAT( CURTIME( ) , '%H:%i:%s' ) returns null
DATE_FORMAT 's first parameter is of type DATETIME. On recent mysql server versions both your queries return NULL.
So the answer to your question is that this difference in behaviour is because of a bug in your mysql version - in some way it converts the TIME to DATETIME, while it cannot convert the string to DATETIME.
Here is also an example of a working query:
select DATE_FORMAT(NOW(), '%H:%i:%s')
NOW() returns a DATETIME while CURTIME() returns TIME.
To my knowledge, I think it's because MySQL recognises the function as a time, and therefore knows how to handle it. Whereas, in the first example, it regards it as a string and doesn't know what to do with it.
I have a string column which acts as a date and I want to select it as a DATE.
Is it possible?
My sample data format would be:
month/day/year -> 12/31/2011
As was told at MySQL Using a string column with date text as a date field, you can do
SELECT STR_TO_DATE(yourdatefield, '%m/%d/%Y')
FROM yourtable
You can also handle these date strings in WHERE clauses. For example
SELECT whatever
FROM yourtable
WHERE STR_TO_DATE(yourdatefield, '%m/%d/%Y') > CURDATE() - INTERVAL 7 DAY
You can handle all kinds of date/time layouts this way. Please refer to the format specifiers for the DATE_FORMAT() function to see what you can put into the second parameter of STR_TO_DATE().
STR_TO_DATE('12/31/2011', '%m/%d/%Y')
Here's another two examples.
To output the day, month, and year, you can use:
select STR_TO_DATE('14/02/2015', '%d/%m/%Y');
Which produces:
2015-02-14
To also output the time, you can use:
select STR_TO_DATE('14/02/2017 23:38:12', '%d/%m/%Y %T');
Which produces:
2017-02-14 23:38:12
http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html
use the above page to refer more Functions in MySQL
SELECT STR_TO_DATE(StringColumn, '%d-%b-%y')
FROM table
say for example use the below query to get output
SELECT STR_TO_DATE('23-feb-14', '%d-%b-%y') FROM table
For String format use the below link
http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_date-format
The following illustrates the syntax of the STR_TO_DATE() function:
STR_TO_DATE(str,fmt);
The STR_TO_DATE() converts the str string into a date value based on the fmt format string. The STR_TO_DATE() function may return a DATE , TIME, or DATETIME value based on the input and format strings. If the input string is illegal, the STR_TO_DATE() function returns NULL.
The following statement converts a string into a DATE value.
SELECT STR_TO_DATE('21,5,2013','%d,%m,%Y');
Based on the format string ‘%d, %m, %Y’, the STR_TO_DATE() function scans the ‘21,5,2013’ input string.
First, it attempts to find a match for the %d format specifier, which
is a day of the month (01…31), in the input string. Because the
number 21 matches with the %d specifier, the function takes 21 as the
day value.
Second, because the comma (,) literal character in the format string
matches with the comma in the input string, the function continues to
check the second format specifier %m , which is a month (01…12), and
finds that the number 5 matches with the %m format specifier. It
takes the number 5 as the month value.
Third, after matching the second comma (,), the STR_TO_DATE()
function keeps finding a match for the third format specifier %Y ,
which is four-digit year e.g., 2012,2013, etc., and it takes the
number 2013 as the year value.
The STR_TO_DATE() function ignores extra characters at the end of the input string when it parses the input string based on the format string. See the following example:
SELECT STR_TO_DATE('21,5,2013 extra characters','%d,%m,%Y');
More Details : Reference