I want to filter some items based on two different dates using MySQL. In my database I store data like 2017-03-28 10:55:10. But I need only the date part, not the time so I used the DATE() function:
select sum(cashamount) as sumcashsalesamount,
DATE(transactiondate) as datepart
from master_transaction
where transactiondate BETWEEN '2017-02-22%' AND '2017-03-28%'
order by transactiondate desc
Above this query have two dates 2017-02-22% and 2017-03-28% but this return no result.
But when I change this 2017-03-28% date to 2017-03-29% (tomorrow date) I get results.
Don't use between with "dates". I put that in quotes, because your values are really datetime values.
The best way to write this condition is:
where transactiondate >= '2017-02-22' and
transactiondate < '2017-03-29'
Note the inequality for the second condition. BETWEEN is inclusive, so it would include midnight on 2017-03-28.
Why is this best?
It allows the query optimizer to take advantage of indexes and partitions.
It is exactly the logic that you want.
It works for both date and datetime types.
BETWEEN is inclusive on both sides, and if no time component is given for a date, it defaults to 00:00:00. Since 2017-03-28 10:55:10 > 2017-03-28 00:00:00 it is not included in the result set.
2017-03-28% = 2017-03-28 00:00:00
If something happened during that day, you need 2017-03-28 23:59:59, or date_sub(2017-03-29, INTERVAL 1 second) for ease
You can only use wildcard characters (%) with LIKE.
Use:
where date(transactiondate) BETWEEN '2017-02-22' AND '2017-03-28'
if you use between and you want today, all day, you must put tomorrow date
And i fix my problem
select sum(cashamount) as sumcashsalesamount
from master_transaction
where DATE(transactiondate)BETWEEN '2017-02-22' AND '2017-03-28'
order by transactiondate desc
I just re-place the DATE(transactiondate) in where condition its worked
Related
how to search between two date , when date format in database like :
2015-10-10 02:23:41 am
i just want to search between two date with format :
2015-10-10
without
02:23:41 am
any ideas please ?
Your question isn't completely clear. But, I guess you hope to find all the rows in your table where Date occurs on or after midnight on 2015-08-05 and before midnight on 2015-09-11 (the day after the end of the range you gave in your question.
Those two search criteria will find all the rows with Date values in the range you specified. (I'm ignoring the 02 at the end of 2015-09-10 02 in your question because I can't guess what it means, if anything.)
Try this query:
SELECT *
FROM table
WHERE `Date` >= '2015-08-05'
AND `Date` < '2015-09-10' + INTERVAL 1 DAY
This has the benefit that it can exploit an index on the Date column if you have one, so it can be fast.
You could write
SELECT *
FROM table /* slow! */
WHERE DATE(`Date`) BETWEEN '2015-08-05' AND '2015-09-10'
That's slightly easier to read, but the WHERE condition isn't sargable, so the query will be slower.
Notice that the beginning of the range uses >= -- on or after midnight, and the end of the range uses < -- before midnight.
Pro tip: Avoid the use of reserved words like DATE for column names. If you make mistakes writing your queries, their presence can really confuse MySQL, not to mention you, and slow you down.
May I suggest:
select * from table where cast(date as date) between '2015-08-05' and '2015-09-10'
When your where clause is based on a timestamp, but you're using date as the parameters for your between, it excludes anything that happens on the second date unless it happened precisely at midnight.
When using the end date for the range, include the time of the end of the day:
SELECT *
FROM YourTable
WHERE date BETWEEN '2015-08-05' AND '2015-09-10 23:59:59'
I want to compare a datetime from sql and the todays date.
The datetime in sql is for example 2015/05/09 12:00:00 and the current date is 2015/05/09. I want to compare these things if they are equal, but for the whole day so the time is a problem. I have the following query.
$q1="SELECT reservations.reservation_id, reservations.room_id, room.room_rate, reservations.arrival_date, reservations.departure_date,
meals.meal_rate, room.room_rate, roomtype.max_persons,
CONCAT(clients.first_name,' ',clients.last_name)as name
FROM reservations, clients, meals, room, roomtype
WHERE reservations.room_id=room.room_id AND reservations.client_id=clients.client_id AND room.roomtype_id=roomtype.roomtype_id
AND reservations.meals=meals.meal_id AND reservations.arrival_date=CURDATE()
GROUP BY reservation_id
";
reservations.arrival_date=CURDATE() this is equal only at 12:00:00 o'clock. i want that to be equal for the whole day.Actually i want to compare if these two dates are equal without time but my database must be datetime with the time...
does anyone has an idea?
thanx in advance
You should learn to use standard join syntax. Simple rule: Never use commas in the from clause.
The answer to your question is either the date() function (or something similar):
where date(reservations.arrival_date) = CURDATE()
Or an inequality:
where (reservations.arrival_date >= CURDATE() and
reservations.arrival_date < date_add(CURDATE(), interval 1 day)
)
The second is actually preferred because it can make use of an index on reservations(arrival_date).
So im gonna try to explain this:
I got datepicker where you select 2 dates (start and end).
For example:
01-01-2012 till 11-12-2012
Now im gonna use these dates to search for jobs in a table:
startdate job: 10-10-2012
enddate job: 20-12-2012
I want to get that job as a result, because the job is running between those 2 dates.
I tried to use BETWEEN but that doesn't seem to work for me. Can somebody help me with the query? Ofcourse i translate the date format to yyyy-mm-dd.
The point is that i want to show the job even if only 1 day is in the range of the datepicker date.
You're looking for the intersection of 2 date ranges. So given dateranges d1 and d2, you know that d2.start must occur before d1.end. You also know that d2.end must occur after d1.start. If both conditions are true, then you have overlap. In SQL:
SELECT d.*
FROM thedates d
WHERE d.jobstart < $myenddate
AND d.jobend > $mystartdate
ETA diagram for clarity:
Simply use the statement as
SELECT * FROM dummydb.tblorders t where t.date_purchased >= '2012-03-24 00:00:00' and t.date_purchased <= '2012-03-24 23:59:59';
In my sql query I output dates in chronological order.
The dates in my database are stored in d-M-Y format.
What I want to do is sort the results by dates equal to or greater than today to be output first.
In my query I have this sort in my query
...From $db ORDER BY STR_TO_DATE(sortdate, '%d-%M-%Y') ASC
Can anyone tell me if I can do a comparison on todays date as each record is output from the db?
This will give me todays date
$todaysdate = date("d-M-Y", time());
but can anyone tell me if I can build that into my query?
Thanks in advance.
check mysql DATEDIFF in combination with CURRENT_DATE ==>
http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_current-date
My guess is that you saved the date in a VARCHAR column. Please don't do that, you make it very complicated for yourself when you want to do stuff (like this) with the date. I'd suggest that you convert the column to a DATE field and then just use:
SELECT * FROM my_table WHERE my_date_field >= CURDATE()
And if you want to output the date in the d-m-Y format, you can use DATE_FORMAT()
You really should be storing the dates in a dateTime format. That will make it much easier to do all sorts of orders, comparisons and plenty of other things. You could for example, then use the mysql now() function to only get the results you need?
...From $db where sortDate>=now() ORDER BYsortdate ASC
Assuming sortdate is datetime field, in order to display dates equal to or greater than today first,could use UNION.
SELECT * FROM my_table WHERE sortdate>= CURDATE()
UNION
SELECT * FROM my_table WHERE sortdate< CURDATE()
You can use WHERE sortdate >= $todaysdate
Just put this condition in where like date_column >= curdate()/$todaysdate
thanks
At the mysql table, there are stored values of timestamps (like 1265138145).
What i want is to display the dates (eg 27/2/2011,10/3/2011,15/3/2011, 16/03/2011 etc) which belong to these timestamps. Is this possible?
(but only display one time the date, eg if there is 1265138145 and 1265138140 then display only one time the date - which is 16/3)
There are a variety of ways of doing this, the FROM_UNIXTIME command probably being the easiest.
For example: SELECT FROM_UNIXTIME(<timestamp field>, '%d/%m/%Y');
I'm not sure what you mean about "only display one time the date", but using DISTINCT on the necessary column should help.
i.e.: SELECT DISTINCT(FROM_UNIXTIME(<timestamp field>, '%d/%m/%Y')); may be all you require.
From within MySQL, use ADDDATE and interval of unixtimestamp seconds to the epoch, e.g.
select adddate('1970-01-01', interval 1265138145 second)
then display only one time the date
Use DISTINCT in your query, e.g.
select distinct date(adddate('1970-01-01', interval 1265138145 second))
from tbl ..
Both queries above return the column as a datetime value, which you can apply default formatting to in PHP.
Note about using FROM_UNIXTIME - you get your local UTC offset added to the time, which is unlikely to be what you want, unless the data was populated using UNIX_TIMESTAMP in the first place.
FROM_UNIXTIME: Returns a representation of the unix_timestamp argument as a value in 'YYYY-MM-DD HH:MM:SS' or YYYYMMDDHHMMSS.uuuuuu format, depending on whether the function is used in a string or numeric context. The value is expressed in the current time zone
<?php
print date("d/m/Y", $timestamp);
?>
http://us.php.net/manual/en/function.date.php
Fetch your data and use just date() function.
echo date('d/m/Y', $row['date']);
use FROM_UNIXTIME(unix_timestamp), FROM_UNIXTIME(unix_timestamp,format) http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_from-unixtime
in mySQL statement