I have installed recently MySQL 5.7 . Something weird is happening with a date column.
If I execute a SELECT query using that field in a WHERE section, I have a resultset, but when I use the same WHERE condition to UPDATE a row I get an Invalid date format error message.
Here is the SELECT sentence:
SELECT *
FROM
FDpoCargTran
WHERE FDpoCargTran.Banco = '001'
AND (FDpoCargTran.Conciliacion = '' OR FDpoCargTran.Conciliacion IS NULL)
AND FDpoCargTran.Fecha = '2016-09-27'
This sentence returns 2 rows resultset, that's ok.
Now, Here's the UPDATE sentence:
UPDATE
FDpoCargTran
SET
Edo = 'C'
WHERE FDpoCargTran.Banco = '001'
AND (FDpoCargTran.Conciliacion = '' OR FDpoCargTran.Conciliacion IS NULL)
AND FDpoCargTran.Fecha = '2016-09-27'
AND Deposito = 1041
And I get this error message:
Data truncation: Incorrect date value: '' for column 'Conciliacion'
The Conciliacion columns is defined as:
`Conciliacion` date DEFAULT NULL,
If I remove the Conciliacion = '' condition, everything works fine.
Why empty string is not valid to evaluate the column in an UPDATE sentence and not in a SELECT?
Please, an idea!!!
Basically, for date datatype, you cannot store something like White
Spaces or ' ' strings. You need to make the column to accept NULL
values and insert an actual NULL into it. This is not a problem in MySQL 5.7, its how date is set in databases.
Update is a DML statement, when you actually want to write something into table, or check for a condition, MySQL is unable to understand what ' ' is for the date column type.
So, you cannot have ' ', instead you can have NULL set. Thats how MySQL can check the condition and make appropriate changes!!
I would suggest, change it to NULL.
Alter your datetime column to varchar.
Import whatever table/database.
Update FDpoCargTran set Conciliacion=NULL where Conciliacion='';
Alter column back to datetime.
Related
i have a column with name 'expdate' with type 'varchar' with values like this:
2021-02-27
28-02-2023
29/02/2024
.
.
.
now i want change all date values like format %d-%m-%Y to the %Y-%m-%d and save to new column with type date and use:
UPDATE `Users`
SET `fixed_expdate` = STR_TO_DATE(REPLACE(`expdate`,"/",'-'), "%d-%m-%Y")
where `expdate` != '0000-00-00'
but sql command show error like:
SQL query:
UPDATE `Users` SET `fixed_expdate` = STR_TO_DATE(REPLACE(`expdate`,"/",'-'), "%d-%m-%Y") where `expdate` != '0000-00-00'
MySQL said: Documentation
#1411 - Incorrect datetime value: '2021-02-27' for function str_to_date
i need to sql command,What is your solution?
You must use different converting expressions for each separate source data format. And you must check that only the values which matches the pattern used in current query are included into the convertion.
This can be performed using a lot of separate queries, each converts one of existing source formats.
-- convert the dates which have correct format
UPDATE `Users`
SET `fixed_expdate` = `expdate`
WHERE `expdate` REGEXP '\d\d\d\d-\d\d-\d\d';
-- convert dated which looks like 28-02-2023
UPDATE `Users`
SET `fixed_expdate` = STR_TO_DATE(`expdate`, "%d-%m-%Y")
WHERE `expdate` REGEXP '\d\d-\d\d-\d\d\d\d';
-- convert dated which looks like 29/02/2024
UPDATE `Users`
SET `fixed_expdate` = STR_TO_DATE(`expdate`, "%d\/%m\/%Y")
WHERE `expdate` REGEXP '\d\d\/\d\d\/\d\d\d\d';
-- and so on
After each separate convertion (or after a lot of convertions) you may look at the rows which were not converted
SELECT `expdate`
FROM `Users`
WHERE `fixed_expdate` IS NULL
LIMIT XXX
and build the next converting expression for a pattern which was not used yet - until all values are successfully converted.
MySQL said: Documentation
#1411 - Incorrect datetime value: '2021-02-27' for function str_to_date
This is due to 2 separate formats which differs in day and month posession (which are swapped) - they cannot be distinguished easily. You must use additional checking if both formats are present in the data. And you cannot distinguish what format must be applied if both formats are possible (for example, when the value is '01/02/2021').
UPDATE (copied from the comment)
Your regular expressions should should probably have anchors for the beginning and end of the string ('^' and '$'). – Gordon Linoff
I have a table with columns
tick varchar(10), category varchar(100)
and they have the following 2 records (for sample)
EFG,0
XYZ,#
(EFG and XYZ) are values for tick column, and (0 and #) are for categories column, respectively.
My need is to append the string ',Cool,' (commas included) to the existing category value, so that the above two records become
"EFG","0,Cool,"
"XYZ","#,Cool,"
So I am running the following SQL statements:
update tablen set category = category + ',Cool,' where tick='EFG';
update tablen set category = category + ',Cool,' where tick='XYZ';
However, for the first SQL statement, it is not changing any value at all. For the second, the category value is updated from # to "0"
In both cases, I am getting warnings:
Warning: #1292 Truncated incorrect DOUBLE value: '#' (and for '0')
Warning: #1292 Truncated incorrect DOUBLE value: ',Cool,'
Unable to understand why the VARCHAR column update is throwing a DOUBLE value error.
Can someone please shed some light on what am I doing wrong here? Is there any typecasting required?
Many Thanks
in mysql the concat is not + but concat()
update tablen set category = concat(category , ',Cool,') where tick='EFG';
update tablen set category = concat(category , ',Cool,') where tick='XYZ';
the plus sign induce mysql to think that the values are number .. but you have varchar ..
I'm trying to update my table column values into a string. My query goes like this
UPDATE tbl_testing
SET result= 'Hey'
WHERE (SELECT (colOne) + '-' + (colTwo) + '-' + (colThree)) = 'r-r-r'
which the columns 'colOne, colTwo and colThree' already contains 'r' but slqyog shows "Truncated incorrect DOUBLE value: 'r-r-r'"
and all of the other result column data became = 'Hey'. What should I do?
You have to decide it is either MySQL or MSSQL.
In MySQL the string concatenation is not + sign, but simply you enumerate the columns separated by comma and the statement is SELECT CONCAT("Field1", "Field2" etc) AS ConcatenatedString); - CONCAT() function.
Try reevaluate your DB engine and adapt the query.
In MSSQL the string concatenation is indeed + sign. Your query works ok in MSSQL and updates the result column with the value you have set.
DDL
CREATE TABLE [dbo].[tbl_testing](
[id] [int] NULL,
[result] [nvarchar](4000) NULL,
[colOne] [nvarchar](4000) NULL,
[colTwo] [nvarchar](4000) NULL,
[colThree] [nvarchar](4000) NULL
) ON [PRIMARY]
GO
INSERT INTO tbl_testing (id, colOne, colTwo, colThree)
VALUES (1, 'r', 'r', 'r')
Update statement
UPDATE tbl_testing
SET result='Hey. I am a concatenated string'
WHERE (SELECT (colOne)+'-'+(colTwo)+'-'+(colThree))='r-r-r'
Output
id result colOne colTwo colThree
1 Hey. I am a concatenated string r r r
Changing comment to answer:
You should avoid doing that statements in that way. By doing that, database use no indexes and also you are giving more computation tasks to database server (server needs to concatenate all values and after concatenations will compare with given string).
Better way is to replace yours where statement with something like:
`WHERE colOne = 'r' AND colTwo = 'r' AND ...`
that will work faster without additional computation need (to concatenate strings).
This solution works much much faster, and looks much much better.
I keep receiving an error message when trying to convert a column, CreatedDate, of string date values in my Estimates table into the mySQL date format using str_to_date(). My column of data contains dates in m/d/yy format (for example: 1/26/16 or 3/3/16).
I ran this query:
UPDATE Estimates
SET CreatedDate = str_to_date( CreatedDate, '%c/%e/%y' )
mySQL is returning this error message:
Error
SQL query:
UPDATE Estimates
SET CreatedDate = str_to_date( CreatedDate, '%c/%e/%y' )
MySQL said: #1411 - Incorrect datetime value: '' for function str_to_date
What is wrong with my query?
Disable NO_ZERO_DATE SQL mode:
set #old_sql_mode = ##sql_mode;
set sql_mode = '';
Run your statement:
UPDATE Estimates
SET CreatedDate = NULLIF(str_to_date(CreatedDate, '%c/%e/%y'), FROM_DAYS(0))
Then enable original SQL modes:
set sql_mode = #old_sql_mode;
Disabling NO_ZERO_DATE mode will make STR_TO_DATE return zero date 0000-00-00 for invalid date strings, the same value is returned by FROM_DAYS(0). So NULLIF will convert zero dates to NULL.
This answer was helpful.
The usual strategy for cleaning up data like this is as follows:
ALTER TABLE Estimates CHANGE COLUMN CreatedDate CreatedDateString VARCHAR(255);
ALTER TABLE Estimates ADD COLUMN CreatedDate DATE
UPDATE Estimates SET CreatedDate=STR_TO_DATE(CreatedDateString, '%c/%e/%y'))
WHERE CreatedDateString IS NOT NULL AND CreatedDateString != ''
Then when you're confident everything got converted correctly:
ALTER TABLE Estimates DROP COLUMN CreatedDateString
The advantage to proper DATE fields is they're in a consistent format and when you add an INDEX on them data retrieval is very fast, even on ranges, like:
SELECT * FROM Estimates WHERE CreatedDate BETWEEN '2016-01-01' AND '2016-06-30'
It's hitting blank values in your column.
SET CreatedDate = str_to_date( '', '%c/%e/%y' )
I think this outputs 0000-00-00 and that works as an invalid date if you are setting a date field to that.
SET CreatedDate = STR_TO_DATE( IFNULL(case when CreatedDate = '' then null else createddate end,'1901-1-1'), '%c/%e/%y' )
That will leave 1901-01-01 values for nulls and blank
Added to tadman:
SET CreatedDate = STR_TO_DATE(case when CreatedDate = '' then null else createddate end, '%c/%e/%y' )
Nulls instead of 1901-01-01 if you prefer.
I've looked all over the internet for my answer, and perhaps I'm just doing things wrong. I have a column in my MySQL table that I need to replace all the NULL values with a text string in my SQL Query using phpMyAdmin. I don't want the output to come out that way, I want to actually replace the null values with the text string.
I've tried
UPDATE `tablename` SET fieldname = replace (fieldname, "", "textstring")
I've read up on
SELECT ISNULL(field,"replacetext)
But this only shows the output, but doesn't actually replace it in the table.
I can't figure this out, and I've wasted so much time trying to find an answer.
update tablename set fieldname = "textstring" where fieldname is null;
Have you tried
UPDATE `tablename` SET fieldname = '' where fieldname is null