Mysql select query after certain date - php

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'

Related

how to search records beetween two dates. But table date format is varchar

I have a table. In which, a column name is tb_date, which is varchar format. In that column dates are save, But in different-different format (Like: 01/07/201 OR 01-08-2018 or 2017/03/12 etc.).
Now I want a search between given date. But it is not working. I tried it-
SELECT * FROM `user_History` WHERE date_format(str_to_date(`tb_date`, '%d/%m/%Y'), '%d/%m/%Y') BETWEEN '01/06/2018' AND '31/06/2018'
But its giving all record.
I tried it in my sql.
Whats is the problem?
Problem is in your str_to_date(tb_date, '%d/%m/%Y'), you give a format %d/%m/%Y of importing date, but you have different formats in this field.
I think, you should process all your table by PHP and, for example, convert all your dates to UNIX_TIMESTAMP by function strtotime().
It will be more easy than try to create a SQL query for it.

MySQL comparing stored date with current date and execute

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')

Selecting records between two dates using PHP from MySql database

In my project , I am generating and storing the Bill (invoice).
The date of Bill is coming to the textbox from the javascript date picker(small pop-up calender) before saving.
The format of the date is : DD-MON-YYYY (18-JUN-2013).
I am using 'Text' data type for storing dates in MySql table.
I have done selecting of records(Previous Bills) from the table by given single date like. . .
$result = mysql_query("SELECT * FROM outward WHERE date='".$date."' ORDER BY billNo");
Now, what i want to do is:
To select records (Bills) between two dates.....
My exact Question is:
Is it possible to query mysql database with this settings or I have to make some changes to select records between 2 dates efficiently ?
How can i achieve this ?
P.s. - Is it effective to use
1. "SELECT * FROM outward WHERE date BETWEEN '" . $from_date . "' AND '" . $to_date . "' ORDER by id DESC"
Or
2. SELECT * FROM outward WHERE date > "15-JUN-2013" and date < "18-JUN-2013"
You could do it in a pure SQL way, but you are going to have to do a full table scan for each query.
select the_dates,
STR_TO_DATE(the_dates, '%d-%M-%Y') as converted
from testing
where STR_TO_DATE(the_dates, '%d-%M-%Y') between '2013-06-20' and '2013-06-23'
Link to SQLFiddle
You should use strtotime PHP function to convert string date to UNIX timestamp format and change MySQL data type for date field to TIMESTAMP.
Than you can do effective queries with > and <.
If it's a DATE column, you can get all dates between 15 June 2013 and 18 June 2013 (inclusive) using this:
WHERE date BETWEEN '2013-06-15' AND '2013-06-18'
If it's a DATETIME column, do this instead:
WHERE date >= '2013-06-15' AND date < '2013-06-19'
If the date column is indexed, this approach will make sure the indexes are available for optimization. If it isn't indexed, the approach is just as fast as the many other ways you can do this.
Addendum: Just saw the "storing as text" amidst all the other shouted info. Note that this answer applies only if the type is DATE or DATETIME. I'll leave it up because the best answer is to change the column's data type and then use this or one of the other suggested options.
I am using 'Text' data type for storing dates in MySql table.
That's a problem. You should store dates as date or datetime data type in MySQL. If you don't care about the time part, date should be sufficient.
If you change your data type to date, then doing:
select x,y,z from table a where a.datecolumn between #startdate and #enddate
Should work fine.
If you use a text data type, you would have to cast the column to a date column and then apply your date selection range which is going to be slower due to the cast.
Always store data in the data type that matches its kind. If a date then a date column, if it's text then text or varchar, etc. The presentation layer of your app can worry about the format in which this data is presented to the user.
You said you were using a TEXT column to store the dates. That's an extremely bad idea. If you switch to a DATE or a DATETIME, then this becomes trivial.
Since you are storing it as text but you want SQL to parse it as a DATE SQL doesn't understand in the first place.
In your example SQL will use TEXT comparison rules. So 15-April < 15-Mar > 15-DEC
If you are storing dates in an SQL database you should be storing it as a Date and not as TEXT.

Wrong search between dates

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.

PHP MySQL date functions

I am working with dates, in both PHP as well as MySQL. EVerytime I use to convert date in unix format. But this time I have taken field in DB as date. But issue is it is taking yyyy-mm-dd format. I want to store it in dd-mm-yyyy format. Is this possible if I set default setting of DB. or each time I have to explode the dd-mm-yyyy format in PHP and convert it in YYYY-MM-DD format. Its my first query.
Second query is I wish to fetch the records from today's date. I mean dates after today's date. Like today then tomorrow then so on.... Is it possible to use order by on date field.
Just use:
$date = date('d-m-Y', strtotime($dateFromDB));
That will convert from MySQL DateTime to the format you have specified.
It is possible to order by date fields, e.g.:
SELECT *
FROM table
WHERE date > [yourDate]
ORDER BY date [DESC | ASC]
Your second requirement contradicts with the first one.
If you store your date in dd-mm-yyyy format, you'll be unable to sort your dates.
So - yes, you have to "explode" the dd-mm-yyyy date in PHP or format it any other way. That's not a big deal though. Everyone does it.
If you have a field of type 'datetime' you can use the MySQL-Command: FROM_UNIXTIME(%d) for conversion. 'order by' should be no problem.
Store the date in default format that is yyyy-mm-dd
when you want to display in front end
use the following query to
select otherFields, date_format(dateField,'%d-%m-%Y') from tableName;
For ordering by date
SELECT * FROM tbl
ORDER BY date DESC

Categories