How to delete string date older then 2020/11 month in MySQL? - 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.

Related

MySQL convert timestamp and time string to DateTime format

In my table have two columns, one as timestamp to save the date and one as time string to save the time with period.
Eg:
I want to combine them into one column as DateTime format in the result then order by desc on that column.
Here is the example: http://sqlfiddle.com/#!9/25eb21/4
The column name 'Datetime' expected is Datetime or timestamps type, so I can sort correctly on it.
You do not need to convert the values to integers to add them. MySQL has built-in functions for this purpose:
SELECT *,
addtime(apptDate, str_to_date(apptTime, '%h:%i %p')) as datetime
FROM appt
ORDER BY Datetime DESC;
If apptTime is just a time value (which it should be), then you obviously do not need to convert from a string. I would usuggest fixing the data model.
Let me assume that you want to add the duration that is stored as a string in column apptTime to timestamp in column apptDate.
A typical approach uses str_to_date() to turn the string to a datetime, then converts the time portion to seconds using time_to_sec(), which we can then add to the timestamp using date artihmetics.
So
select t.*
apptdate
+ interval time_to_sec(str_to_date(appttime, '%h:%i %p')) second
as newapptdate
from mytable
select addtime(appDate, appTime) from ...
Your appDate contains a time, probably because you are applying a timezone. Either convert your two columns to the timezone your data is supposed to be in with convert_tz(), or extract the date part of it with date(appDate) before you add it. It wasn't clear which of the columns was a string, but extract() or str_to_date() is the way to parse a text into a date and/or time.

Mysql - Change verbose date format to Y-m-d

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.

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 order by closest date/hour before or after

I have a mysql table that has a column that stores dates but isn't in the date format, it's a varchar.
The column is called data_hora and have dates in the dd/mm/yy format, example: 06/09/2012 15:00, so I had to convert to date format in mysql query.
And I need to get the closest date and hour before or after the current time, I came up with the following code, but for some reason it seems to get only closest date but not hour, weird?!?!
SELECT str_to_date(data_hora, '%d/%m/%Y %H:%i') AS data_hora
FROM requisicoes
ORDER BY abs(DATE_FORMAT(NOW(),'%d/%m/%Y %H:%i') - data_hora) LIMIT 1
Help :(
try this:
Your ORDER BY Clause has to be changed
SELECT str_to_date(data_hora, '%d/%m/%Y %H:%i') AS data_hora
FROM requisicoes
ORDER BY abs(TIMEDIFF( NOW() , str_to_date(data_hora, '%d/%m/%Y %H:%i'))) LIMIT 1
You shouldn't format NOW() to a string: it is already a TIMESTAMP value; instead, take the (absolute) difference between the present UNIX_TIMESTAMP() and that of the data_hora alias for your selected STR_TO_DATE() column:
ORDER BY ABS(UNIX_TIMESTAMP() - UNIX_TIMESTAMP(data_hora))
If at all possible, I would advise altering your schema so that your data_hora column is stored as a TIMESTAMP: it will greatly improve the performance of queries of this sort.

Mysql date interval

I am trying to select rows which are within a certain time period:
$sql = mysql_query(" SELECT * FROM table WHERE ... AND
date BETWEEN ('$date' - Interval 2 YEAR) AND ('$date' + Interval 2 YEAR)
OR date ISNULL");
The format of the dates saved in this column are mm/dd/yyyy
a. how do I select the time interval? currently it is selecting all the rows, not just those within this date range.
b. Am I using the corret syntax to make sure we include all null options as well?
Update.
Following the answers and comments I changed the column type to "Date" (it was varchar), and have also changed the current type of the existing entries to the correct date format.
I now have 2 questions:
a. This is still not working... What else might be the problem?
b. I want to change in PHP where the entry is saved. How ahould I go about this? Is the following correct:
$new_date = mysql_query("STR_TO_DATE($old_date_string,'%m/%d/%Y')");
Solution.
First, I changed the column type to DATE (from VARCHAR).
Second, I moved and converted all the existing entries from their existing column and type (str) to new type (date) and new column.
Third, I added parentheses to the "date" section.
Fourth, I changed ISNULL to IS NULL - addin a space, which changes it from a function to a statement.
Fifth, I am now updating my script so that in the future dates will be saved to the new column in the new type, converting them to the correct fprmat before saving them to the database.
Thankyou #nnicholas and everybody.
If you are really storing your dates as strings I suggest you update your table with something like the following -
-- add a new column of type DATE
ALTER TABLE `table` ADD COLUMN `date_new` DATE AFTER `date`;
-- populate the new column from the old one
UPDATE `table` SET `date_new` = STR_TO_DATE(`date`,'%m/%d/%Y');
-- drop the old column
ALTER TABLE `table` DROP COLUMN `date`;
-- rename the new column
ALTER TABLE `table` CHANGE `date_new` `date` DATE;
After making these changes handling dates will be much easier for you. When inserting the dates that you are receiving you simply use STR_TO_DATE('new_value','%m/%d/%Y') to convert the date during the insert -
mysql_query("INSERT INTO `table` (field1, field2, `date`)
VALUES('value1', 'value2', STR_TO_DATE('$old_date_string','%m/%d/%Y')");
This assumes that $old_date_string has already been sanitised.
With your table structure updated the standard date arithmetic functions will work as intended.
$sql = mysql_query("SELECT *
FROM table
WHERE ...
AND (`date` BETWEEN ('$date' - INTERVAL 2 YEAR) AND ('$date' + INTERVAL 2 YEAR) OR `date` IS NULL)");
$date must be in Y-m-d format.
You can't do date comparisons on it if it's not stored in the database as a date.
You could do something very ugly casting it to a date with STR_TO_DATE() and then comparing but I'd suggest reworking your table structure to store this date as a DATE object.
SELECT * FROM table WHERE ... AND
STR_TO_DATE(date, '%m/%d/%Y') BETWEEN ('$date' - Interval 2 YEAR) AND ('$date' + Interval 2 YEAR)
OR date ISNULL
This assumes that $date is in the correct MySQL DATE format of 2012-03-14 (for 14th March 2012)
You have to CAST/CONVERT your date field that you are getting from MySQL (you really called a date field 'date'? that's a bad idea).
The best answer is to convert your date column to an actual DATE type. You should also not really use the mm/dd/yyyy format as its ambiguous.
You need to enclose the entire date checking part in brackets, like so:
$sql = mysql_query(" SELECT * FROM table WHERE ... AND
(
date BETWEEN ('$date' - Interval 2 YEAR) AND ('$date' + Interval 2 YEAR)
OR date ISNULL
)");
And it should work as expected.
Apart from the missing brackets, your condition seems to take into account NULLs all right (i.e. NULLs would be included in the output regardless of the argument passed).
UPDATE
It seems I was mistaken in presuming that the date column was the date type. #James C has got the point, as well as #nnichols, and their suggestion about changing the column type seems the way to go in your situation.