mysql update deletes the field data on some rows rather than update them when i run this mysql statement
UPDATE
tablename
SET
date = DATE_FORMAT(STR_TO_DATE(date, "%d/%m/%Y"), "%Y-%m-%d")
I am trying to update date formats from 05/08/2013 to 2013-05-08 to make it so I can search where date between $date and $date
try this:
UPDATE tablename SET `date` = STR_TO_DATE(`date`, '%d/%m/%Y')
and then change the datatype of the column like this
ALTER TABLE tablename CHANGE COLUMN `date` `date` DATE
OR ALternatively
update tablename SET date = DATE_FORMAT(STR_TO_DATE(date,'%d/%m/%y'),'%y-%m-%d')
You should make sure that your date field is true DATE or DATETIME datatype. After that,
UPDATE tablename
SET `date` = STR_TO_DATE(old_date, '%d/%m/%Y')
Then, you would be able to do all normal math on dates: add, subtract, compare, sort, etc.
MySQL STR_TO_DATE() returns a datetime value by taking a string and a specific format string as arguments.
If the date or time or datetime value specified as string is illegal, the function returns NULL so check all value in select statement first to know if its returning null
Select DATE_FORMAT(STR_TO_DATE(date, "%d/%m/%Y"), "%Y-%m-%d") from tablename
Related
I have a column with value 9/12/2012 00:00:00.000000 in a MySQL table, how do I update the table to make the value like 2012-12-9 00:00:00.000000.
The best thing to do would be to convert your text date column to a proper datetime column using STR_TO_DATE:
UPDATE yourTable
SET newDate = STR_TO_DATE(oldDate, '%m/%d/%Y %H:%i:%s.%f');
Once you have done this, you may view your datetime column any way you want:
SELECT DATE_FORMAT(newDate, '%Y-%d-%m %H:%i:%s.%f') AS newDateFormatted
FROM yourTable;
In my database the column type is varchar(30) which stores date (24/02/2018), having multiple records.
i want the maximum date e.g i have 10/01/2016, 20/03/2017, 24/02/2018.
I am using the below query:
SELECT MAX(receipt_date) as rd FROM tblname
which returns me 10/01/2016 which is wrong.
i also tried to convert it to date format. but failed. mysql gives me syntax error.
SELECT CONVERT(varchar, mycolumn, 105) FROM tblname
but failed.
This is what I'd do:
ALTER TABLE tblname ADD COLUMN receipt_date2 DATE;
UPDATE tblname SET receipt_date2 = STR_TO_DATE(receipt_date, '%d/%m/%Y');
ALTER TABLE tblname DROP COLUMN receipt_date,
CHANGE receipt_date2 receipt_date DATE;
SELECT MAX(receipt_date) AS rd FROM tblname;
You can't use dates effectively in MySQL if you store than as VARCHAR in dd-mm-yyyy format. You should use a DATE data type. MySQL's DATE data type stores dates in yyyy-mm-dd format only. This way it can search for MAX() easily, it can sort them, it can do date arithmetic.
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 have a mysql trigger that updates a table when there's an insert in another table. My question is this, how can i convert the current date to bigint value, because it's the bigint value that needs to be updated. This is the update statement
UPDATE clocks SET last_clock_upload = NOW() WHERE clock_id = NEW.clock_id
How can i change the NOW() to bigint?
Try this:
SELECT UNIX_TIMESTAMP(NOW( ) )
I have one column date1 which is varchar type
I want this column to date type.
I tried changing field but all date is converted to 0000-00-00.
format is dd-mm-yyyy but in varchar.
How can I convert the same date format but with date format using sql queries or similar but at database level ?
UPDATE `table`
SET `column` = str_to_date( `column`, '%d-%m-%Y' );
More about STR_TO_DATE function.
Since your column name is date1, you can replace column with date1 in the above syntax, and the code shall be:
UPDATE `table`
SET `date1` = str_to_date( `date1`, '%d-%m-%Y' );
The other answers here are risky, because if they go wrong you'll lose your data. A safer way to do this is to create a new field on your database with a DATE (or DATETIME if you need time as well) format, then to run a query like
UPDATE `table` SET `my_new_date_field` = STR_TO_DATE( `my_old_data_field`, '%d/%m/%Y');
In this way, if the %d/%m/%Y bit is wrong, you won't lose your data.
Once you're happy, you can delete the old data field and rename the new one.
use STR_TO_DATE Function of MySQL
FIRST you will need to update the value in date format.
UPDATE `tbl` SET `date1` = STR_TO_DATE(`date1`, '%d-%m-%Y') WHERE 1=1
THEN Convert the field to date.
Most importantly remember to insert date as Y-m-d format, after then.