Selecting rows from database between two dates giving wrong results, the below query not working for me. I tried some answers, but this one not giving the correct results.I think, i am missing somewhere.
SELECT * FROM Table WHERE Date BETWEEN '07/10/2015' AND '07/14/2015'
changed to
SELECT * FROM Table WHERE Date BETWEEN '07-10-2015' AND '07-14-2015'
still not working!
That's right, you can not use BETWEEN statement when the data type format is not a DATE or DATETIME, you must change the data type first.
BTW I just realized that even data type is a DATE/DATETIME format you can't use / in the SQL statement itself when using MySQL, versus SQL you can use / when the column data type is DATE/DATETIME. Just correct me if I'm wrong...
Use the default date format YYYY-MM-DD
SELECT * FROM your_Table
WHERE Date BETWEEN '2015-07-10' AND '2015-07-14'
Erm, you're searching on "Date" but are you meant to be searching on "CreateDate"? That's what your image shows.
Related
I have a table with field name counselorDate which have value with date and time.But i want to get data between to dates only(not with time).I used following query.But its not worked. please help me
SELECT
*
FROM poolMainEnqDetails
WHERE counselorDate
LIKE BETWEEN ('2014-01-01%' AND '2014-01-03%')
The syntax of the BETWEEN is flawed, it should be
BETWEEN '2014-01-01%' AND '2014-01-03%'
Also, what does your sql management tool (PHPMyAdmin, SQL Workbench, etc...) tells you ? If it doesn't work, it should either give an error, either give a result you're not expecting.
If you compare a date against a datetime the that will be converted to datetime.
With that in mind you'll get the folowing where clause:
SELECT
*
FROM poolMainEnqDetails
WHERE counselorDate >= '2014-01-01'
AND counselorDate < '2014-01-04'
I'm trying to query a database with a between 2 dates... The problem is the column that I am querying contains dates that are currently formatted like this "01/01/2014" (dd/mm/yyyy) with a column type of VARCHAR.
At the moment, I can't convert the column to a date type.
Basically when I query the table because it's not set to date type the between query doesn't return the correct rows...
Has anyone else come across this problem, is there something I can change within the query?
$this->db->where('IssueDate >=', '02/12/2013');
$this->db->where('IssueDate <=', '22/01/2014');
$query = $this->db->get('MYTABLE');
Thanks guys.
The solution is to use str_to_date():
$this->db->where("str_to_date(IssueDate, '%d/%m/%Y') >=", "'2013-12-92'");
$this->db->where("str_to_date(IssueDate, '%d/%m/%Y') <=", "'2014-01-22'");
$
You may not have any control over the database. But you do have control over your own constants. You should get used to the ISO standard YYYY-MM-DD for such constants -- unambiguous and accepted correctly by most databases.
I might suggest creating a view on the table in the database that transforms the string date columns you have into the following format... YYYYMMDD
That format is sortable and can easily be compared versus other similar formatted dates - you can even do date arithmetic with it.
Keep in mind that a view does not copy the table or add any performance overhead. It is often a good idea to access any table through a view even if initially you do not need to perform any manipulations on the underlying table - it will help if you later find you do need to perform them.
use BETWEEN clause with STR_TO_DATE(). Check below code:-
$wh = STR_TO_DATE(`IssueDate`,'%d/%m/%Y')." between '02/12/2013' and '22/01/2014'";
$this->db->where($wh);
Expect it'll give You perfect result.
I'm having some troubles dealing with Timestamp data type in MySQL.
I'm saving simple records in my database using a simple DB structure, like:
ID int
Name varchar
Date timestamp
Text varchar
And then retrieve them with something like:
SELECT * FROM table WHERE Date BETWEEN '2013-01-01' AND '2013-06-30'
Everything works fine if I store records letting MySQL fill the Date field with the actual timestamp, for example: 2013-10-04 22:40:02 which means I don't add any value to the Date field in my INSERT query.
But I need to be able to add the date by my self since my application needs to store the date from where the application started, and not the date and time in which the query was sent to the database.
So what I do is I create the same date/time format my Date field uses which is 2013-10-04 22:40:02 and then do a simply insert:
INSERT INTO table (Name, Date, Text)
VALUES ('Peter', '2013-10-04 22:40:02', 'Hello...')
Now, doing it this way I'm unable to bring any result by date using a select query like this one:
SELECT * FROM table WHERE Date BETWEEN '2013-01-01' AND '2013-11-30'
Even if I try to sort results by Date using PHPMyAdmin interface, all the records that contain manually added dates disappear. If I sort them by ID, they re-appear. I checked and the dates and formats are correct. So I have no idea what the problem could be. I'm new at MySQL by the way.
Hope you can give me a hand. Thanks!
Well, I think I found the problem and it has nothing to do with PHP and MySQL, the problem is that I generate the date with JavaScript, and it's giving the wrong month.. :/
Thanks to everyone anyway!
I am creating a mysql db with a php frontend. The data it will use is extracted from another larger db and contains a date/time field which looks like this - 20120301073136 - which records when an event happened.
I understand that this might be a UNIX timestamp? Not sure.
I want to be show this field in the tables in my PHP webpage as a readable date and time -
ie something like 01-Mar-2012 07:31:36 or similar
Should I try and convert it with SQL command or let PHP format it? And, what is the code to do so?
BTW, it is important that I can sort the data (in SQL and in the PHP table) into date order - ie in the order that these events happened.
Thanks in advance for your help - Ive learnt a lot here already
J
You can convert it to a datetime directly in your SQL query. Example:
select cast(20120301073136 as datetime)
You can also order that with no need to convert it since it is a number in the format YYYYMMDDHHmmss
select * from yourTable
order by yourDateTimeField
You should make use of the MYSQL DATE functions. Check the docs before asking simple questions. http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html.
Also you can sort the dates directly in your query using ORDER BY.
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'