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
Related
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..
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
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')
I am trying to convert a database with all values stored as VARCHAR into the correct column type eg: INT, DATE, DECIMAL etc...
Problem is, that the columns date_order, date_billed and date_paid are stored in different formats
I still get a 1292 Truncated incorrect date value: '05/18/2011' errors and have pretty much no clue what to do since that exact date format is listed as CASE
My code:
SELECT
CAST(`ar_no` AS UNSIGNED),
CAST(`accession_id` AS UNSIGNED),
CAST(`client_id` AS UNSIGNED),
CAST(`insurance_id` AS UNSIGNED),
CAST(`test_id` AS UNSIGNED),
CASE
WHEN `date_paid` = '0' THEN `date_paid` = '00/00/0000'
WHEN LENGTH(DATE(STR_TO_DATE(`date_order`, '%m/%d/%y'))) IS NOT NULL THEN STR_TO_DATE(`date_order`, '%m/%d/%y')
WHEN LENGTH(DATE(STR_TO_DATE(`date_order`, '%m/%d/%Y'))) IS NOT NULL THEN STR_TO_DATE(`date_order`, '%m/%d/%Y')
END,
CASE
WHEN `date_paid` = '0' THEN `date_paid` = '00/00/0000'
WHEN LENGTH(DATE(STR_TO_DATE(`date_billed`, '%m/%d/%y'))) IS NOT NULL THEN STR_TO_DATE(`date_billed`, '%m/%d/%y')
WHEN LENGTH(DATE(STR_TO_DATE(`date_billed`, '%m/%d/%Y'))) IS NOT NULL THEN STR_TO_DATE(`date_billed`, '%m/%d/%Y')
END,
CASE
WHEN `date_paid` = '0' THEN `date_paid` = '00/00/0000'
WHEN LENGTH(DATE(STR_TO_DATE(`date_paid`, '%m/%d/%y'))) IS NOT NULL THEN STR_TO_DATE(`date_paid`, '%m/%d/%y')
WHEN LENGTH(DATE(STR_TO_DATE(`date_paid`, '%m/%d/%Y'))) IS NOT NULL THEN STR_TO_DATE(`date_paid`, '%m/%d/%Y')
END,
CAST(`amount_billed` AS DECIMAL(15,2)),
CAST(`amount_received` AS DECIMAL(15,2)),
CAST(`amount_adjusted` AS DECIMAL(15,2))
FROM `acs`.`billing_unformatted`;
Mysql only excepts dates in YYYY-MM-DD format. That is why you are getting errors such as 1292 Truncated incorrect date value: '05/18/2011'
You can change this in Excel before doing your import.
In order to change the date format in excel: right click on the top cell. Choose format cells from the drop down list. change the local to something like 'Afrikans'. Choose the format that looks like 2001-03-14. Use the top cell to fill down. Then save the document.
Just a quick note: Excel sometimes tries to do too much and will revert this column back to a the English(U.S) default time zone. So, if you plan on doing more editing make sure that the column has not reverted back.
Here is a link to more string literals on dev.mysql.
This stored procedure and post might help you as well: Error code 1292
I have a column called "Time" this column is currently set to Varchar since i have some corrupted Time in some updates.Some Time has non ASCII characters etc. So how can i sort out all these corrupted and non properly formated time fields and set to NULL ? So that i can safely convert the Time column back to DateTime. The normal time fields in the updates are usually in the format of 2013-07-24 14:37:56
I was thinking of sorting it out by doing something like :
SELECT * FROM updates WHERE TIME not LIKE '....-..-.. ..:..:..'
But i don't know if that is the right regex approach and most efficient.
You could try using MySQL STR_TO_DATE on your varchar column:
update updates set `time` = null
where str_to_date(`time`,'%Y-%m-%d %H:%i:%s') is null;
So how can i sort out all these corrupted and non properly formated time fields and set to NULL ?
If CAST of such TIME column returns a NULL, then you can set it to NULL or a desired date time string value.
Example with Select:
mysql> select #ts:=cast( '2013-07╞ƒ~¥14:37:56' as datetime ) ts, #ts is null;
+------+-------------+
| ts | #ts is null |
+------+-------------+
| NULL | 1 |
+------+-------------+
Example for update:
update table_name
set time_coumn = null -- or a valid date time string value
where cast( time_column as datetime ) is null
Once you have set the column value to a NULL, you can then set valid datetime values for those records which have NULL values.
SELECT * FROM updates WHERE DATE_FORMAT(`TIME`, '%Y-%m-%d %H:%i:%s') IS NULL
Function DATE_FORMAT will try to format TIME in 'Y-m-d H:i:s' format, if function is not able to format TIME it will return NULL, so that's how you'll know which one is not in appropriate format and then you can handle them manually or set them to null with UPDATE query