I'm working on a project which was apparently built by a total beginner. The date is being stored in a varchar column in the format 'Jan 20, 2010'. I need to convert this column to a DATE or DATETIME but when I do so (on a backup), the values becomes 0000-00-00.
I tried to convert the values to proper format before changing the column type using str_to_date() and DATE() functions but both report invalid string format. Is it possible to convert this data to a valid date format?
Use STR_TO_DATE, and update this column with a proper date value using the current text date.
UPDATE yourTable
SET new_date = STR_TO_DATE(old_date, '%b %e, %Y');
My guess is that either you are using the wrong format mask and/or some of your text dates have problems. Here is a brief demo showing that the above logic works.
Demo
Use this instead.
UPDATE table_name
SET DATES = DATE_FORMAT(DATES, '%Y-%m-%d');
I suggest to use another column to store the updated date value. I.E: update tb_date set new_date_column = DATE_FORMAT(date_column, '%y-%m-%d');
Check it out, Hope it'll work for you.
Related
So I'm trying to insert dates into a table and the date is in this format:
8/3/2021
However I want to add a leading 0 before the month and day so the date shows 08/03/2021. Also I want to add it as a string concatenated with another string so test123-08/03/2021
If you really store date in that format then you may try this:
SELECT
DATE_FORMAT(STR_TO_DATE(date_col_string,'%d/%m/%Y'),'%d/%m/%Y') as 'zero-padded',
CONCAT(string_val,'-',DATE_FORMAT(STR_TO_DATE(date_col_string,'%d/%m/%Y'),'%d/%m/%Y')) as 'concatenated'
FROM mytable;
Use STR_TO_DATE() function to change the date value to standard MySQL date format of YYYY-MM-DD then use DATE_FORMAT() function to display the date value as per your desired output. The second operation is adding CONCAT() function on the converted date with your selected string. I'm assuming that your date value is d/m/y, because as #Stu mentioned in the comment, since you're not storing as MySQL standard date format, that means 8/3/2021 can be either d/m/y or m/d/y. With a standard date format value, the query would be shorter:
SELECT
DATE_FORMAT(date_col,'%d/%m/%Y') as 'zero-padded',
CONCAT(string_val,'-',DATE_FORMAT(date_col,'%d/%m/%Y')) as 'concatenated'
FROM mytable;
Demo fiddle
You should be inserting your source dates into a proper date or datetime column. Then, to view your dates in the format you want, use the DATE_FORMAT() function with the appropriate format mask:
SELECT DATE_FORMAT(date_col, '%d/%m/%Y') AS date_out
FROM yourTable;
In my table, there is a date column i.e. ORDER DATE which is in dd-mm-yyyy format, and the datatype of this date column is text. Now, I want to convert the format from yyyy-mm-dd. But the date format is not changing at all. Trying to use STR_TO_DATE but the year in the dates are coming as 2020 only. Not sure how to fix it or if there is a better way to convert the date in MySQL.
For example:
SELECT STR_TO_DATE('14-09-2017','%d-%m-%y') AS DATE;
Result coming as 2020-09-14
Or suppose
SELECT STR_TO_DATE('20-MAR-2018','%d-%b-%y') AS DATE;
Result coming as 2020-03-20
day and month are coming properly but the year is not coming. Kindly help how to get convert the date format correctly.
Try to change your command like this below and you will get the answer:
SELECT STR_TO_DATE('14-09-2017','%d-%m-%Y') AS DATE;
Refer to the documentation for more info about the commands.
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.
I'm trying to convert a string to date with a format of yyyy/mm to yyyy
STR_TO_DATE(SUBSTRING(time,1,4), '%Y')
This returns null for every value as well as when I try to convert without getting rid of the mm:
STR_TO_DATE(REPLACE(time,'/',''), '%Y%m')
This method has worked for me before, I'm at a loss for what I'm missing.
Thanks in advance!
Edit for clarification:
Eventually I am going to insert the year into a column with data type year, so I need to convert a varchar to a date type so I can extract the year in order to insert the data into a new table
I am in the process of making sure it will work before populating the new column with a command like this:
INSERT INTO table (year)
SELECT STR_TO_DATE(SUBSTRING(time,1,4), '%Y')
FROM `origtable`
I'm trying to convert a string to date with a format of yyyy/mm to yyyy
Just use left():
select left(time, 4) as yyyy
There is no need to convert to a date or datetime. You want a simple string operation.
EDIT:
year is a really weird type. I would just use an integer. But if you are using it, you can insert a full date into the field:
select date(concat(time, '/01'))
The resulting date can be inserting to a year column.
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.