I have stored dates (dd/mm/yyyy) in text format in a table in a field called dates. I want to compare those dates with the current date and if the dates are smaller (if the dates have passed) to move the entire row into a new table called archive. Tried something with the DATEDIFF() but I'm new to MySQL and can't figure it out.
I'm going to preface my answer with a short remark: storing "date" values in SQL database in VARCHAR columns is an anti-pattern. MySQL provides native datatype DATE which is designed to handle "date" values. But that's just a remark, doesn't answer your question.
You can use the convenient MySQL STR_TO_DATE function to convert strings into DATE values. For example:
STR_TO_DATE('15/05/2015','%d/%m/%Y')
You could use a column reference in place of the literal, e.g.
STR_TO_DATE(t.mycharcol,'%d/%m/%Y')
and that will return a DATE value you can compare to another DATE value, using the standard inequality operator < for example.
To return the current date from the database, you can use an expression such as
DATE(NOW())
Putting that together, you could write a query like this:
SELECT t.*
FROM t
WHERE STR_TO_DATE(t.mycharcol,'%d/%m/%Y') < DATE(NOW())
If you want to take the result from a SELECT statement and insert those rows into another table, you can use the INSERT ... SELECT form of the INSERT statement.
Reference: https://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_str-to-date
Beware of the behavior with badly formatted or invalid dates, e.g.
SELECT STR_TO_DATE('35/05/2015','%d/%m/%Y')
, STR_TO_DATE('15-05-2015','%d/%m/%Y')
Related
I need to select data from mysql for last 7 days. I have field named 'date' and which have values in mm.dd.yy format.
So i tried to find special mysql request to do that, but its not work with my field, i gues that beacause date in wrong format.
How i can do that from php (use some variable to get mysql entries), or with custom select query ?
You can use STR_TO_DATE() to convert your idiosyncratic date format to a standard DATE value. An expression like this will do the trick
STR_TO_DATE('07.17.97', '%m.%d.%y')
Then you can say
WHERE STR_TO_DATE(`date`, '%m.%d.%y') >= CURDATE() - INTERVAL 7 DAY
in your query to filter items with date values starting a week ago.
But, if you have a lot of rows to filter you will have poor performance: this kind of WHERE clause is not sargable.
First read your table and change the date format
$new_date_format = date('Ymd',mktime(0,0,0,substr($date,0,2),substr($date,3,2),substr($date,6,2)));
After that you can make comparisions
I have a SQL table where one of the columns 'creator' is a VARCHAR data type. That column stores values like this one: Mike Jones|05-17-2015 1:21 pm consisting of the person that created the record, the date and time it was created.
I need to use the date portion of that varchar data to form a SQL query, so that we can generate a list of tickets created between two dates. We can't restructure the table easily so i'm stuck working this out in its current format, otherwise we just would have added a CREATED date field and compared on that.
The SQL being attempted is this:
SELECTticknumb,cnames,creator,ticktype,tickstatusFROMaticketsWHEREcreator>= '%05-24-2015%' ANDcreator<= '%05-30-2015%' ANDtickstatusNOT LIKE 'Closed,Complete'
Is there a way to do this directly within the SQL query without having to to a looped explode in the php first to extract and compare the earliest / latest dates?
Here is SQL snippet how you can convert your value in varchar column to date type.
SELECT STR_TO_DATE(SUBSTRING_INDEX(`creator`, '|', -1), '%m-%d-%Y %h:%i %p')
http://sqlfiddle.com/#!9/710a2/7/0
Another inspiration
how to convert a string to date in mysql?
How to split the name string in mysql?
I hope it is what you are looking for.
I have a PHP scirpt that is always querying all the data from a database table and it's getting pretty slow. I really just need the data of a specific month and year.
Is there a simple way to get only those entries? For example, everything from February 2013?
The column that stores the dates in my table is of type datetime, if that applies to the solution.
You can add that condition in the WHERE clause of your select statement. I would recommend using BETWEEN operand for two dates:
SELECT myColumns
FROM myTable
WHERE dateColumn BETWEEN '2013-02-01' AND '2013-02-28';
If you mean to say you want everything beginning with February 2013, you can do so using the greater than or equal to operator:
SELECT myColumns
FROM myTable
WHERE dateColumn >= '2013-02-01';
EDIT
While the above are my preferred methods, I would like to add for completeness that MySQL also offers functions for grabbing specific parts of a date. If you wanted to create a paramaterized query where you could pass in the month and year as integers (instead of a start and end date) you could adjust your query like this:
SELECT myColumns
FROM myTable
WHERE MONTH(dateColumn) = 2 AND YEAR(dateColumn) = 2013;
Here is a whole bunch of helpful date and time functions.
You should index the datetime field for added efficiency and then use Between syntax in your sql. This will allow the mysql engine to remove all records that you are not interested in from the returned data set.
In the database I have table users which have column 'birthday'
Here is the sql query that I use to fetch the results
SELECT *
FROM (`users`)
WHERE `birthday` between '12/11/1982' and '31/01/1983'
The problems is that I get records only for year 1982?
Notice that records for 1983 year exists.
What could be the problem here ?
This is just an alternative answer from Mark Byers. If the data type of your column birthday is varchar and has format of dd/MM/yyyy, please do read this; otherwise, read from Mark Byer's.
The first thing you need to do is to convert your column into DATE or DATETIME datatype by using STR_TO_DATE,
SELECT *
FROM `users`
WHERE STR_TO_DATE(`birthday`,'%d/%m/%Y')
between '1982-01-12' and '1983-12-31'
SQLFiddle Demo
SOURCES
STR_TO_DATE
DATE formats
You should put the year first in your date literals:
SELECT *
FROM `users`
WHERE `birthday` between '1982-11-12' AND '1983-01-31'
From the documentation:
Date and time values can be represented in several formats, such as quoted strings or as numbers, depending on the exact type of the value and other factors. For example, in contexts where MySQL expects a date, it interprets any of '2015-07-21', '20150721', and 20150721 as a date.
If you are using a varchar type then it still makes sense to put the date first (both in the data and in the query) because then your dates will sort correctly when using the default sorting order.
I am trying to pull records after a certain date using mysql query , the field type is date in my database and the query is
SELECT * FROM tickets WHERE created_on > 26-08-2011
But it is not working and also showing all before that date
Thanks
The date you are using is a string, so it needs to be placed inside quotes. Also, the format is the wrong way around:
SELECT * FROM tickets WHERE created_on > '2011-08-26'
For more information, see the MySQL docs. In particular, note the very first line:
The format of a DATE value is 'YYYY-MM-DD'. According to standard SQL,
no other format is permitted.
The date is defined in yyyy-mm-dd, so you should use the date as 2011-08-26.
Using a date in this format is ideal for sorting as the numbers are arranged as incremental pieces.
You have to use quotes on string values, see the post of James Allardice.
Try using quotes on the date and write dates in yyyy-mm-dd format for best results. '2011-08-26'