I need to filter records by time only, so I'm applying this query to MySQL
SELECT * FROM TABLE WHERE TIME(created_at) >= '07:00:00' AND TIME(created_at) <= '06:59:59';
This should filter all records between 24 hours, but no output, however if I change time like below query it works perfectly
SELECT * FROM TABLE WHERE TIME(created_at) >= '00:00:00' AND TIME(created_at) <= '23:59:59';
I've tried to get difference between two given time slots and I'm getting result like this
SELECT TIMEDIFF('00:00:00','23:59:59');
gives me -23:59:59 hours of difference in total
while
SELECT TIMEDIFF('07:00:00','06:59:59');
gives me just 00:00:01, but I suppose it should be -23:59:59 like above
What's the better way to fix this simple problem!?
It has a day crossing, so solution would be something like this
SELECT * FROM TABLE WHERE ((TIME(created_at) >= '07:00:00' AND TIME(created_at) <= '23:59:59') OR (TIME(created_at) >= '00:00:00' AND TIME(created_at) <= '06:59:59'));
Related
I have two columns.
CREATED_DATE || CREATED_TIME
And I want to write a query to get records between these columns.
I wrote below query but it fails because of time column.
SELECT *
FROM TABLE
WHERE (CREATE_DATE BETWEEN '1982-10-21' AND '2015-02-25')
`AND (CREATE_TIME BETWEEN '14:00:00' AND '15:00:00')
However if requested_start_date >= created_date and requested_end_date =< created_date it then SQL should not compare time columns.
How can i handle it?
Thank you
Regeards
select *from TABLE
where CAST(CREATE_TIME as time) >= '14:00:00'
or CAST(CREATE_TIME as time) < '15:00:00'
try this for time comparision
I have a Mysql Table that is used for a log file on the that table there is a field called 'log_date' And it stores the date in the following format( %Y-%m-%d %H:%i.%s ).On the DB the dates look like something this 2013-20-05 00:00.00. Lets say today's date is 2013-20-05 And I have log files from 2013-01-01 to present day. If I run a query like this:
SELECT * FROM log_table
WHERE STR_TO_DATE(log_date, '%Y-%m-%d %H:%i.%s') < '2013-05-05 00:00.00'
This is returning every row in the DB including rows that are greater than 2013-05-05 00:00.00
And if I reverse the < (less then) to a > (greater then) with a query that looks like this:
SELECT * FROM log_table
WHERE STR_TO_DATE(log_date, '%Y-%m-%d %H:%i.%s') > '2013-05-05 00:00.00'
Then it returns ZERO rows. I think the time stamp is what is causing the problem I have worked with the date format before but not the DateTime format. Why is this happening?
log_date should be of DateTime data type. It is much simpler to use MySQL DATE function. Some examples
SELECT * FROM log_table
WHERE DATE(log_date) < '2013-05-05'
SELECT * FROM log_table
WHERE DATE(log_date) > '2013-05-05'
SELECT * FROM log_table
WHERE DATE(log_date) BETWEEN '2013-04-05' AND '2013-05-05'
SELECT * FROM log_table
WHERE DATE(log_date) BETWEEN DATE(CURRENT_DATE() - INTERVAL 2 WEEK) AND
DATE(CURRENT_DATE() + INTERVAL 4 DAY)
You can try with that..
WHERE DATE_FORMAT(AUCTION_DATE, '%Y%m%d') >= DATE_FORMAT('2013/5/18', '%Y%m%d')
You can also get today date using now() function.
I'm working on a project where I want to display data after 3 days have passed.
What I'm having an issue with is getting the current date dynamically in php/sql. I'm aware of how to get the current date in php, but I dont know how to compare that value to the date that I have in the sql database.
You can do that directly in SQL
select * from your_table
where date_column <= curdate() - interval 3 day
You can use an interval select to limit the records to within 3 days if the column you're checking istimestamp, date, or datetime.
select * from tablename where timestamp_column >= NOW() - INTERVAL 3 DAY
You can use DATEDIFF function to check for days.
SELECT *
FROM tablename
WHERE DATEDIFF(CURRENT_DATE(), datecol) >= 3;
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 !!!