I try to get following query to work, but 0 rows are affected:
UPDATE table_x
SET sql_date = DATE(STR_TO_DATE(date_string,'%Y-%m-%d'))
WHERE sql_date = '0000-00-00'
The date format in the column date_string is: '%d.%m.%Y'
What am I doing wrong?
Thanks
your column date_string is already "yyyy-dd-mm", then why you want to update.
Still you can compare date as per below:
UPDATE table_x set sql_date = DATE(STR_TO_DATE(date_string,'%Y-%m-%d'))
WHERE date(sql_date) = '0000-00-00';
SO it will skip time stamp and compare only date with your passes date.
Date function will get only date from the stored date. So if "1992-11-06 00:00:00" is stored in DB then it will compare only "1992-11-06".
You say date_string is in the format '%d.%m.%Y'. But you are mistakenly specifying '%Y-%m-%d' for STR_TO_DATE instead. Moreover when you have converted the string to date you apply DATE on this date. Why?
UPDATE table_x
SET sql_date = STR_TO_DATE(date_string, '%d.%m.%Y')
WHERE sql_date = '0000-00-00';
Related
I am trying to convert dates in the format mm/dd/yyyy to the standard date format yyyy-mm-dd using the STR_TO_DATE function. Some fields in my date column are null and some contain a date.
For instance, 8/22/2011 should become 2011-8-22.
When I select my date column, it looks like this:
8/22/2011
8/10/2010
5/12/2012
etc.
I tried using the code
UPDATE table SET date = STR_TO_DATE(date, '%m/%d/%Y')
which filled the column with NULL values. Also tried
UPDATE table SET date = STR_TO_DATE(#date, '%m/%d/%Y')
with same result, although this time I did not get a warning message.
The first one is correct:
UPDATE table SET date = STR_TO_DATE(date, '%m/%d/%Y')
But if the date is not valid (not in %m/%d/%Y format) then it returns NULL
Try executing and then showing warnings. It tells you what is wrong:
UPDATE table SET date = STR_TO_DATE(date, '%m/%d/%Y');
SHOW WARNINGS;
Maybe some dates are not in format %m/%d/%Y (posibly %d/%m/%Y)
I need to update a column with datatype as timestamp. I run the following query:
update job_info set ProcessStartTime = UNIX_TIMESTAMP(CURDATE()) where JobID=4;
But it updates with a value : 0000-00-00 00:00:00 What could be the reason for this? Is the query incorrect?
Don't use UNIX_TIMESTAMP because MySQL UNIX_TIMESTAMP() returns a Unix timestamp in seconds or your column type is datetime
update job_info set ProcessStartTime =CURDATE() where JobID=4;
or use NOW()
update job_info set ProcessStartTime =NOW() where JobID=4;
I have a table with field Timestamp(DateTime format). I need to get elements that are entered on the current date. Kindly suggest me a way/query to do so. How to fetch current current date out of the timestamp and compare it with current date via query. HELP!!!
You can use the function curdate() function
select * from table_name where date(col_name) = curdate();
Or even better when you have large volume of data and the timestamp column is index and you want index to take into account which will not when you use date() function you can do as
select *
from table_name
where col_name between concat(curdate(),' 00:00:00') and concat(curdate(),' 23:59:59');
I want to update date field in my table. I am currently getting date value from the form like mm/dd/yy i.e(05/06/13) format. I want the date to convert into Y-m-d (i.e 2013-05-06) format and to update it. I used DATE_FORMAT but the date is not updating .
UPDATE tableName
SET coldate = DATE_FORMAT(STR_TO_DATE(colDate,'%m/%d/%y'),'%Y/%m/%d')
SQLFiddle Demo
I have a table which is populated with data from an external source. The problem is I am getting two different format of dates for the same column, there are some records in '%Y-%m-%d %H:%i' format and other in '%Y-%d-%m %H:%i%s' format and they are all VARCHAR type.
I know how to convert a string type date field to a date/datetime field but how do I handle discrepancies in the way the dates are coming? Is it possible to Update dates on the basis of their individual format, so that I can apply a WHERE condition for updates to take place ONLY WHERE date_field is of '%Y-%m-%d %H:%i' format, and then another query to make updates for date field in '%Y-%d-%m %H:%i%s' format?
Right now when I try updating the tables with a common query I get error for the fields which donot match the format:
UPDATE my_table
SET my_date_field = STR_TO_DATE(my_date_field,'%Y-%m-%d %H:%i:%s');
RESPONSE: Error Code: 1411. Incorrect datetime value: '10-22-12 15:00' for function str_to_date
UPDATE my_table
SET my_date_field = STR_TO_DATE(my_date_field,'%Y-%d-%m %H:%i');
RESPONSE: Error Code: 1292. Truncated incorrect datetime value: '2010-01-01 00:00:00'
for this value 10-22-12 15:00 you should use, %y-%d-%m %H:%i
while for 2010-01-01 00:00:00, it should be %Y-%m-%d %H:%i:%s
so your query will use CASE
UPDATE my_table
SET my_date_field = (CASE WHEN CHAR_LENGTH(my_date_field) = 14
THEN STR_TO_DATE(my_date_field,'%y-%d-%m %H:%i')
ELSE STR_TO_DATE(my_date_field,'%Y-%m-%d %H:%i:%s')
END)
See SQLFiddle Demo
Other Source
DATE Formats
you can not update like that because if there is data in that column and it is of different type then you can not update table column so you should delete the column or make a procedure for that in that you can check and the replace or update table.