Is there a way to Select records between dates and specific time range. For example all the records from 2013-11-01 to 2013-11-30 between hours 05:00 to 15:00. Here what i made until now.
select count(*) as total from tradingaccounts accounts
inner join tradingaccounts_audit audit on audit.parent_id = accounts.id
where date(audit.date_created) between date('2013-11-01 00:00:00') AND date('2013-11-01 23:59:59')
But how am I going to set the specific time range?
You can use the HOUR function to add an additional condition on the hours:
select count(*) as total from tradingaccounts accounts
inner join tradingaccounts_audit audit on audit.parent_id = accounts.id
where date(audit.date_created) between date('2013-11-01 00:00:00') AND date('2013-11-01 23:59:59')
AND HOUR (audit.date_created) BETWEEN 5 AND 15
As others answered, HOUR() could help you.
But HOUR() or DATE() cannot use INDEX. To make query faster, I suggest that add time_created TIME column and save only TIME part. after that ADD INDEX(date_created, time_created). finally with below query, you can retrieve rows with high speed.
where audit.date_created between '2013-11-01 00:00:00' AND '2013-11-01 23:59:59'
AND audit.time_created BETWEEN '05:00:00' AND '15:00:00'
Replace your DATE function, it skips time part. Use TIMESTAMP instead.
add following line in query and set your hour in BETWEEN
and time(audit.date_created) between '00:00:01' AND '01:00:00'
If you use date(audit.date_created), the index on date_created field could not take effect.
Just simply use where audit.date_created >= 'xx-xx-xx' and audit.date_created < 'xx-xx-xx'
Related
I have a simple query that means to count all OId's from a table for the current day. I want to put it in to Crystal Reports so users can generate a report daily that counts all results for the day they run it.
The code as i first figured would be:
SELECT COUNT(oid) from TABLE_A where DATE = CURDATE()
But CURDATE() only returns results with 00:00:00 time stamp. So i get all results with 2015-08-06 00:00:00 as the date.
How do i get all results for the day regardless of time stamp?
Thanks in advance!
Mickey!
You can write your query in a sargable way as
SELECT COUNT(oid)
from TABLE_A
where DATE >= concat(CURDATE(),' 00:00:00')
and DATE <= concat(CURDATE(),' 23:59:59')
For Current Date & Time
$CurrentDateTime=date("d-m-Y h:i:s");
=> 06-08-2015 15:33:21
For Current Date
$CurrentDate=date("d-m-Y");
=> 06-08-2015
I am not getting result till '2013-07-31' instead I get one day before endDate.
How can I add one day in '2013-07-31'
select * from employee
where admissiondate>='2013-01-01'
and admissiondate<='2013-07-31'
2013-07-31 12:00:00 is neither smaller nor exactly equal to 2013-07-31.
To fix it either you strip off the time part from your datetime column using date()
select * from employee
where date(admissiondate) between '2013-01-01' and '2013-07-31'
But that won't make use of indexes. Or add a time part like this
select * from employee
where admissiondate >= '2013-01-01'
and admissiondate <= '2013-07-31 23:59:59'
SQLFiddle demo
Use the DATE_ADD() function ...
MySQL DOC
I try to select all date between to dates/times. Unfortunately i get no resolute. I have seen a lot of examples where people try to select data between ex. '2012-08-27' without the time. I need the time as well.
Here is my query. Can anyone tell me whats wrong? The row date has the timestamp as datatype.
SELECT date, points
FROM nf_publicpoints
WHERE date BETWEEN '2012-08-27 00:00:00' AND '2012-08-02 00:00:00'
ORDER BY points DESC
LIMIT 10
Thanks in advance
Troels
try using DATE function
WHERE DATE(date) BETWEEN '2012-08-02' AND '2012-08-27'
if you need time,
WHERE date BETWEEN '2012-08-02 00:00:00' AND '2012-08-27 23:59:59'
there is no syntax issue on that query.
I think you simply have to turn around your query like so:
SELECT date, points
FROM nf_publicpoints
#canged that || ||
WHERE date BETWEEN '2012-08-02 00:00:00' AND '2012-08-27 00:00:00'
ORDER BY points DESC
LIMIT 10
The time has to be ginven in increasing order, as there is no time between 27 and 02 of August, but there is one between 02 and 27.
Hope this helps
I am trying the following but get no results:
SELECT *
FROM users_test
WHERE dateadded >= UNIX_TIMESTAMP('2012-02-01 00:00:00')
AND dateadded < UNIX_TIMESTAMP('2012-11-01 00:00:00');
Yet I know there are columns with dates within that range e.g.
2012-05-11 17:10:08
Is there a better way to do this?
Eventually I want to search multiple parameters, albeit not at the same time, like today, yesterday, last week, last month etc and also a date range and month range
Have you tried?
SELECT *
FROM users_test
WHERE dateadded >= '2012-02-01 00:00:00'
AND dateadded < '2012-11-01 00:00:00'
For what I can see, it seems your table has the data stored in the same way you want to look for it (2012-05-11 17:10:08), so in this case you won't need UNIX_TIMESTAMP.
Also I can see you want to exclude the 2nd date from results (because you're using < instead of <=), otherwise using WHERE dateadded BETWEEN '2012-02-01 00:00:00' AND '2012-11-01 00:00:00' would be fine as well...
Just use the SQL BETWEEN keyword. That's all.
try this:
SELECT * FROM
users_test
WHERE
dateadded BETWEEN '2012-02-01 00:00:00' AND '2012-11-01 00:00:00'
SELECT * FROM table_name WHERE DATE(date_field) between '2015-05-10' and '2015-05-21`
I have a SELECT query where I want to find all rows whose DATE and TIME are between 2011-12-11 23:00:00 and 2011-12-12 23:00:00 I try to do it with WHERE but row is empty
WHERE (date >= '2011-12-11' AND time > '23:00:00' )
AND (date < '2011-12-12' AND time < '23:00:00' )
Pls, any good suggestion how to change this?
You could use:
SELECT * FROM table WHERE DATETIME(date) BETWEEN '2011-11-11 23:00:00' AND '2011-12-13 23:00:00'
or separate:
SELECT * FROM table WHERE DATETIME(date) > '2011-12-11 23:00:00' AND DATETIME(date) < '2011-12-13 23:00:00'
EDIT:
I am not sure I understand what you are trying to achieve here or how your DB is laid out but assuming date and time are separate fields:
SELECT * FROM table WHERE DATETIME(concat(DATE(date),' ',TIME(time))) BETWEEN '2011-11-11 23:00:00' AND '2011-12-13 23:00:00'
I haven't tested but this may work.
Yup, that's pretty much not going to work. Show me all rows where time is greater than 11 pm and time is less that 11 pm. Time and Date are different fields?
You'll have to be a little more clever building up the query:
WHERE (date = '2011-12-11' AND time > '23:00:00' )
or ( date = '2011-12-12' AND time < '23:00:00' )
for a 24 hour window, you just need to have 2 clauses. If you want more than a 24 hour window, you'll need three clauses, one for the start date, one for the end date and one for all the dates in between:
WHERE (date = '2011-12-11' AND time > '23:00:00' )
or ( date = '2011-12-13' AND time < '23:00:00' )
or (date >='2011-12-12' and date < '2011-12-13')
ha, and I have the solution without rebuild the dbase - it's working :))
WHERE
CONCAT(date,' ',time) >= '2011-12-11 23:00:00'
AND
CONCAT(date,' ',time) < '2011-12-12 23:00:00'
Maybe it helps for someone.
thanks for all helping people, brgs
hard to tell without the complete query. also assuming that the date column is actually a date type(?) you would usually do something like TO_DATE('2012-12-11','yyyy-mm-dd') to convert to date types in the comparison.
Let's make sure of certain things
You need to get rid of the idea of separate date and time fields when searching
You need to create an additional column in your table called date_time (type DATETIME) which combines the two fields.
You should probably ditch the separate date and time fields and have just date_time
You can then create an index on date_time
Here is the command to do that
ALTER TABLE yourtable ADD INDEX date_time (date_time);
Once you do these things, THEN you can create a query with a WHERE clause that looks like this:
WHERE date_time >= '2011-12-11 23:00:00'
AND date_time < '2011-12-12 23:00:00'
If you cannot combine the date and time fields, you can still create an index
ALTER TABLE yourtable ADD INDEX date_time (date,time);
Given that situation, you can create a query with a WHERE clause that looks like this:
WHERE (date >= '2011-12-11' AND time >= '23:00:00')
AND (date <= '2011-12-12' AND time < '23:00:00')
The EXPLAIN plan for either situation should result in a fast execution of the query with the use of the date_time index.
Give it a Try !!!