I'm using MySQL version: 5.7.22
So I'm trying to have a table that contains a date column from string. The text field contains data in following DateTime format "d/m/YYYY h:m:s" format. e.g. "14/11/2018 20:10:04 +00:00".
I want to alter the table with a new column that is of the following format '%Y-%m-%d'. I get a
Data truncation: Truncated incorrect date value error
when I try to update the table. But I get the result when I just use a select statement to convert from string to date.
UPDATE BIG_DATA SET BIG_DATA.RealDate = ( SELECT x.d
From (SELECT (DATE_FORMAT(STR_TO_DATE(BIG_DATA.Date , '%d/%m/%Y'), '%Y-%m-%d')) as d
FROM BIG_DATA) as x);
Any help would be grateful!
The reason you are getting an error is that the warning for an incorrect date value (one that produces a value with zeros in it) that is produced by STR_TO_DATE on a SELECT is promoted to an error when you attempt to do an UPDATE. For example, if you do
SELECT STR_TO_DATE('14/11/2018 20:10:04 +00:00', '%d/%m/%Y');
SHOW WARNINGS
Your output will be:
2018-11-14
Warning 1292 Truncated incorrect date value: '14/11/2018 20:10:04 +00:00'
You can work around this by only supplying the date part (the leftmost 10 characters) of the string to STR_TO_DATE:
SELECT STR_TO_DATE(LEFT('14/11/2018 20:10:04 +00:00', 10), '%d/%m/%Y');
SHOW WARNINGS
Output is simply 2018-11-14
This then allows you to create your other column and UPDATE it from the date column:
ALTER TABLE big_data ADD
realdate DATE;
UPDATE big_data
SET realdate = STR_TO_DATE(LEFT(date, 10), '%d/%m/%Y');
SELECT * FROM big_data
Another possibility you might want to consider is using a generated column:
ALTER TABLE big_data ADD
realdate DATE AS (STR_TO_DATE(date, '%d/%m/%Y'));
SELECT * FROM big_data
In both cases the output is
date realdate
14/11/2018 20:10:04 +00:00 2018-11-14
Demo on dbfiddle
Related
My data looks like this:
Year Month Country Sales Dates
2018 March Malta 5000 2018 March
Trying to concatenate Year and Month in one field.
CREATE TABLE data (
Year INT,
Month VARCHAR (15),
Country VARCHAR (40),
Sales FLOAT,
Dates DATE
);
SET GLOBAL sql_mode = '';
UPDATE data SET Dates = STR_TO_DATE(Dates, '%Y %M');
LOAD DATA INFILE 'data.csv' INTO TABLE data
FIELDS TERMINATED BY ';'
LINES TERMINATED BY '\n'
IGNORE 1 ROWS;
I also tried to use
UPDATE data SET Month = STR_TO_DATE(Month, '%M');
But either way I am getting :
Error Code: 1292. Incorrect date value: '2000 January' for column 'Dates' at row 1
(same to Month AND while in Command Line client and Workbench)
So I created additional column manually, but it looks like I cannot format it the way it's necessary.
If I used
UPDATE data SET Dates = str_to_date(concat(Year,' ',Month), '%Y %M');
without the manually created column, MySql returned:
Unknown column 'Dates' in 'field list'"
Using MySql Workbench 8.0.30
When I run DATE_FORMAT('test', '%W %M %Y') I get null returned.
I'm running an update to my table extras where the column is a nullable varchar, but when I run eg.
update extras
set extras.`value` = DATE_FORMAT('test', '%W %M %Y');
I get the following error:
[22001][1292] Data truncation: Incorrect datetime value: 'test'
extras.value is a varchar column with datetime values some of which are not valid dates. I want to update the column to null when the datetime is invalid ie. just a string as in this case 'test'.
Check does the value is valid date with regular expression.
Example - the most simple pattern which does not check for value validity (and allows, for example, '2022-25-78', in this case the whole UPDATE will fail):
CREATE TABLE test (
id INT AUTO_INCREMENT PRIMARY KEY,
src_value VARCHAR(255),
dst_value VARCHAR(255)
);
INSERT INTO test (src_value) VALUES ('test'), (CURRENT_DATE);
SELECT * FROM test;
id
src_value
dst_value
1
test
null
2
2022-11-22
null
UPDATE test
SET dst_value = DATE_FORMAT(src_value, '%W %M %Y')
WHERE src_value REGEXP '^\\d{4}-\\d{2}-\\d{2}$' ;
SELECT * FROM test;
id
src_value
dst_value
1
test
null
2
2022-11-22
Tuesday November 2022
fiddle
When the STRICT_TRANS_TABLES sql_mode is enabled, any date/time parsing error becomes a fatal error. You have to disable it or use a regex to validate the date string (which is very messy) before using it as a date.
fiddle
I am trying to analyze order_Date column and column have multiple date format i want to convert all those date in same format which wull make be easier to analyze the order_date.
I am trying to analyze the order_date however this column have multiple date format 2019/07/15 and 1/13/2014
Howeever, while converting different format date with one format yyyy/mm/dd with query.
select date_format(order_date, '%y/%m/%d'),orderid from superstore;
it shows null values like this.
i have tried to use `CAST as well but it shows every single value as null.
select case when order_date like '%Y' then date_format(order_date, '%Y/%m/%d') else null end as newdate from superstore;
date_format funtion is used to format a date datatype you should use https://dev.mysql.com/doc/refman/8.0/en/date-and-time-functions.html#function_str-to-date any null values returned by str_to_date either failed or started as null. You will need to examine these and adjust the str_to_date parameters appropriately. There is a catch though is 20/2/20 y/m/d or d/m/y (for example) and how can you differentiate month and day where both are <=12?
For example
drop table if exists t;
create table t
(dt varchar(10));
insert into t values
('1/1/2020'),('2020/1/12'),('12/12/12'),(null),('13-14-15');
select dt,
case when length(substring_index(dt,'/',-1)) = 4 then str_to_date(dt,'%d/%m/%Y')
when length(substring_index(dt,'/',1)) = 4 then str_to_date(dt,'%Y/%m/%d')
when length(substring_index(dt,'/',1)) = 2 then str_to_date(dt,'%y/%m/%d')
else str_to_date(dt,'%y/%m/%d')
end dateformatted
from t;
https://dbfiddle.uk/?rdbms=mysql_8.0&fiddle=553219f33ad9e9a4404fc4c0cb6571c9
note in no case can I identify month and day and sometimes year..
Hi ALL I am trying to get date difference between two dates. I am getting data from cube and saving in temp table. I tried to defined date field as a varchar and also datetime and date
My date is in the format 03/04/18
And I am trying DATEDIFF(DAY, PAAR.DateReceived, O.DateReceived) for getting number of days difference. But I am getting error as below.
When I defined date field in temp table as DateTime or Date
"Operand type clash: ntext is incompatible with datetime"
When I defined date field in temp table as Varchar then
"Conversion failed when converting date and/or time from character string."
Thanks In Advance
I suggest you use str_to_date() with the appropriate format modifiers to convert those strings into dates.
DATEDIFF(str_to_date(PAAR.DateReceived,'%d/%m/%y'), str_to_date(O.DateReceived,'%d/%m/%y'))
see: https://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_str-to-date
Try this
set #sdate = '01/01/18';
set #edate = '01/26/18';
set #sdate = CONCAT_WS('-',SUBSTR(#sdate FROM 7 FOR 2),SUBSTR(#sdate FROM 1 FOR 2),SUBSTR(#sdate FROM 4 FOR 2));
set #edate = CONCAT_WS('-',SUBSTR(#edate FROM 7 FOR 2),SUBSTR(#edate FROM 1 FOR 2),SUBSTR(#edate FROM 4 FOR 2));
SELECT DATEDIFF(date(#edate), date(#sdate));
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)
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'
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')