MYSQL same table has different date formats - mysql

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.

Related

MySQL - Converting TEXT datatype (yyyy-mm) to DATE

I have a column called "month" that I have imported into MySQL:
Datatype: TEXT
Display: 2021-01
My goal is to convert this column into some sort of DATE datatype (ie. DATE, DATETIME) so that MySQL will recognize it as yyyy-mm.
I have tried the following method but still received NULL
--- (1) add dummy date
SELECT CONCAT(month, '-01')
FROM tablename;
--- (2) convert to DATE datatype
SELECT CONVERT(month,DATE)
FROM tablename; -- if I just run (1) and (2) i receive NULL
--- (3) Format back to yyyy-mm format
SELECT FORMAT(month,'yyyy-mm') AS month2
FROM tablename; -- If i run from (1) to (3), i receive 2,021
So:
How do i solve this problem conversion problem in MySQL
Is there anyway i could have prevented this before/while importing?
If you are looking for a one time migration from your string format to a SQL date or datetime, then you can just do
UPDATE tablename SET columname = CONCAT(columname, '-01');
and then update the column to a DATE or a DATETIME.
Alternatively if you do not want to update the values you could use the following queries to cast to a DATE or a DATETIME:
SELECT CAST(CONCAT(columnname, '-01') AS DATE) FROM tablename
or
SELECT CAST(CONCAT(columnname, '-01') AS DATETIME) FROM tablename

How to delete string date older then 2020/11 month in MySQL?

I have a database, with around 500k rows, I don't know why but instead of using "Date" type on the column it uses "varchar". Now the date has a format - 01/02/2021 07:08:49 AM
My question is how should an SQL query look to delete this kind of "old" date rows from the table? Or in another hand how should I convert the column without losing the data and holding the same format to a Date type column?
I tried deleting with something like this:
DELETE FROM `visited` WHERE LEFT(`last_visit_date`, 2) != '01' OR LEFT(`last_visit_date`, 2) != '12';
However, this didn't fully clean the table.
Any help would be appreciated.
You may use STR_TO_DATE in your delete query to convert the text dates to bona fide dates:
DELETE
FROM visited
WHERE
STR_TO_DATE(last_visit_date, '%d/%m/%Y %h:%i:%s %p') < '2020-11-01';
Note that if your text dates actually have month before day, then use this call to STR_TO_DATE:
STR_TO_DATE(last_visit_date, '%m/%d/%Y %h:%i:%s %p')
If you don't want to lose the dates with the wrong format, you can update the table:
UPDATE visited
SET last_visit_date = STR_TO_DATE(last_visit_date, '%d/%c/%Y %r')
WHERE last_visit_date LIKE '__/__/____ __:__:__ __';
and change the data type of the column to DATETIME (if all the other values of last_visit_date are valid datetimes):
ALTER TABLE visited MODIFY last_visit_date DATETIME;
See a simplified demo.

Change date string with "am/pm" to actual date in MySQL

I have a column of dates written as strings with "am" and "pm" at the end, for example:
1/1/2016 12:00:00.000 AM
The type of the column is currently varchar, I want to change the type to datetime format.
Attempt 1
Changing the column type from phpmyadmin dashboard gives the following error:
Query error:
#1292 - Incorrect datetime value: '1/1/2016 12:00:00.000 AM'
Attempt 2
Considering the format of the date, I tried to use the STR_TO_DATE function. But it does not give the expected result. It instead returns the same date for each row.
SELECT STR_TO_DATE(`assembling-machine-tag`.`Time`, "%Y") from `assembling-machine-tag`
Result
So, how can I change the format of my column from varchar (with format = dd/mm/yy hh:mm:ss.ms AM) to datetime (with format = yyyy-mm-dd hh:mm:ss) in MySQL 7.4.1?
You just need to use the correct pattern:
SELECT STR_TO_DATE('1/2/2016 01:02:03.456 AM', '%e/%c/%Y %h:%i:%s.%f %p')
-- 2016-02-01 01:02:03.456000
In order to convert varchar data to datetime, you first need to create a temporary datetime column and update it like so:
UPDATE t SET `datetimetemp` = STR_TO_DATE(`varchardate`, '%c/%e/%Y %h:%i:%s.%f %p')
Once satisfied with result drop, the varchar column and rename the temporary column.
I would use this version:
SELECT STR_TO_DATE('1/1/2016 12:00:00.000 PM', '%d/%m/%Y %h:%i:%s.%f %p')

How to convert text datatype to datetime in mysql?

In mysql database,column name created.This "created " column is text datatype,I need to change this to datetime.Now this column have so many datas.Is it possible to convert it or?
Database look like
created
18-11-15 18:21:25
Expecting ouput is
created
2018-11-15 18:21:25
When am doing
ALTER TABLE invoices MODIFY created datetime
This query giving wrong data.its converting from 15-09-18 03:03:43 to 2015-09-18 03:03:43
If the original data is not in MySQL Datetime format (YYYY-MM-DD HH:MM:SS), you cannot just change the column datatype from Varchar/Text to Date/Datetime. Otherwise, there will be an irreparable Data loss.
This will be a multi-step process. You will first need to convert the date string to MySQL date format (YYYY-MM-DD HH:MM:SS). We can use STR_TO_DATE() function for this.
Your sample date string (18-11-15 18:21:25) is basically in %y-%m-%d %T format. Following format specifiers can be used:
%d Day of the month as a numeric value (01 to 31)
%m Month name as a numeric value (00 to 12)
%y Year as a numeric, 2-digit value
%T Time in 24 hour format (hh:mm:ss)
The query to update the date would look as follows:
UPDATE invoices
SET created = STR_TO_DATE(created, '%y-%m-%d %T');
Now, you can use Alter Table to change the data type from Text type to Datetime.
ALTER TABLE invoices
MODIFY COLUMN created datetime;
The best thing to do here is to not store your dates as text. Assuming you have already done this, we can cope by calling STR_TO_DATE to generate a bona fide date:
SELECT
STR_TO_DATE(created, '%y-%m-%d %h:%i:%s') AS created_out
FROM yourTable;
Since the output you expect is standard date output, we can stop here and avoid also calling DATE_FORMAT to generate a different output.
you want to convert output or database records ? for second you can use sql query :
UPDATE 'table_name' SET 'created' = CONCAT('20', 'created')
You will need first to interchange the day with the year in the created column, as follows:
UPDATE invoices
SET created = CONCAT(SUBSTR(created, 7, 2), '-', SUBSTR(created, 4, 2), '-', SUBSTR(created, 1, 2));
Then, you convert the column to DATETIME, as follows:
ALTER TABLE invoices MODIFY created DATETIME;
Hope this helps.

MySQL STR_TO_DATE returning null

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)