MySQL Return Today's Records Using DATE from DATETIME Field - mysql

I have a table called "actions" with a DATETIME column called "occurred". I'm trying to return the records for today by doing the following
SELECT * FROM `actions` WHERE `occurred` = DATE(NOW());
But I get an empty result set. If I take the WHERE clause out, I can see all 295 rows in the table and there's at least 30 rows from today. Later I will be writing another query to return all records between today's date and X amount of days in the past but before I can get there I need to know why this query is returning an empty result set.
Thanks in advance.

SELECT * FROM actions WHERE DATE(ocurred) = CURDATE();
DATE(ocurred) ignores the time part.
Here's the SQL Fiddle to play with the data: http://www.sqlfiddle.com/#!2/81708/2

If there in no future date in occurred, you could just use below:
SELECT * FROM `actions` WHERE `occurred` > DATE_SUB(CURDATE(), INTERVAL 1 DAY);

Related

MSAccess: Return query for specific date from datetime value

I am trying to run a query from a linked table in MS SQL Server that has a datetime field. I'm trying to run a simple query to search for records for a specific date (#03/24/2018#), but since the datetime field has the time as well, I am not getting any records unless I specify the time range using BETWEEN with the time (Between #03/24/2018 00:00:00 AM# And #03/24/2018 11:59:59 PM#).
Original query which does not return desired output:
SELECT *
WHERE MyDateTimeField) = #3/24/2018#;
Query
SELECT *
WHERE MyDateTimeField) Between #3/24/2018 00:00:00 AM# And #3/24/2018 23:59:59#);
Is there a workaround to this as to not have to use a BETWEEN operator with the time?
To avoid time portion, check forMyDateTimeFieldequal/greater than searched day and less than next day:
SELECT *
FROM MyTable
WHERE
MyDateTimeField >= #3/24/2018#
AND
MyDateTimeField < DateAdd("d",1,#3/24/2018#);
In opposite of convertingMyDateTimeFieldto date, this does not prevent index usage and handlesNullValues onMyDateTimeField.

SQL query doesn't work on a specific day

I have a weird problem in mysql!
my query is
SELECT * FROM aa WHERE problemTime>= '2016/03/20' AND problemTime<= '2016/04/20'
the result of this query is nothing , but when I change the first time to 2016/03/19 or 2016/03/21 I have the following result! I mean these queries
SELECT * FROM aa WHERE problemTime>= '2016/03/21' AND problemTime<= '2016/04/20'
or
SELECT * FROM aa WHERE problemTime>= '2016/03/19' AND problemTime<= '2016/04/20'
the result in both time ( 19th and 21th) is
but when I use 20th the result is noting
my main table is
I change the format of time from 2016/03/20 to 2016-03-20 ( I mean change / to - ) but it doesn't have change too!
whats the problem?
You should really be running a query like this if your problemTime column is datetime type:
SELECT * FROM aa
WHERE
problemTime>= str_to_date('2016/03/20', '%Y/%m/%d') AND
problemTime <= str_to_date('2016/04/20', '%Y/%m/%d')
Don't rely on implicit conversions between string and date.. leave your table data alone and ensure you explicitly convert your where clause parameters to the same data type as in the table. Also remember that a date "without" a time is actually midnight on the day in question, and midnight is like zero, it's the first thing that happens on any given day. A time of 6am on a given date, is after midnight, so a query that asks for dates less than or equal to midnight on a particular date means the 6am date will be excluded
This is general good DB practice; do not convert table data where possible, because it can cause huge performance hits and wrong results
Your column "problemTime" have date with time. Do not convert table data, change your where clause (add time).
SELECT * FROM aa WHERE problemTime>= '2016/03/20 00:00:00' AND problemTime<= '2016/04/20 23:59:59'
Try this as per SQl Server.
SELECT * FROM aa
WHERe cast(problemTime AS date) between '2016/03/21' AND '2016/04/20'

mysql filter by date and time separately

I have to perform a query on a MySQL database.
I have a table with records, have a column called "date" (the date type), and a column called "time" (type. Integer is stored by multiplying the time of day by 60. eg 8 am is stored as 480).
Unfortunately, the format of this table can not be modified.
My table stores attentions of doctors on call. The doctors on duty working in two shifts: from 8-20, and 20-8.
I need to know the amount of attention for every doctor.
My query must be filtered by date range and shift.
The problem is that, in the case of doctors working at the turn of 20-8, I have to consider a change of day. (sorry for my bad English).
What I have done is this, this would be an example to date of yesterday, and doctors shift 20-8.
SELECT * FROM attentions WHERE (date >= '2015-07-23' and time >=1200) and (date <= '2015-07-24' and time <480)
the query does not work at all.
Supposing the date field is called: 'a_date' with format 'yyyy-mm-ss' and the time field is a number, the query should be:
SELECT * FROM attentions WHERE (date(a_date) >= '2015-07-23' and time >=1200) and (date(a_date) <= '2015-07-24' and time <480)
Can you check using between?
SELECT * FROM attentions WHERE date between '2015-07-23' and '2015-07-24' and time between 1200 and 480
I think you can also use this -
SELECT * FROM ***** where CREATED_DATETIME between '2015-03-12 00:00:00' and '2015-05-11 00:00:00';

fetch last 5 minutes of data from oracle table

Hi i am trying to fetch last 5 minutes of data from oracle table.The query is written below and its not working somehow.
select * from mytable where (time_to_sec(timediff(now(),mytable.time_stamp)) <= 300)
Its showing this error ORA-00904.
I tried one more query.
select * from mytable where TIME_STAMP > (sysdate - numtodsinterval(5,'minute'))
Now, can you tell me the query which fetches data of last 5 minutes and which deletes data that is in the table for more than 12 hours.Thanks.
I need queries in both oracle and mysql. The mysql query i tried is here.
delete from mytable where (time_to_sec(timediff(now(),time_stamp))/3600 >12);
In oracle subtracting 1 from timestamp means one day. And You can substract a fraction of one. So,
current_timestamp - (5/(24*60))
gives You date from 5 minutes ago. Using that we can query:
select * from mytable where TIME_STAMP > current_timestamp - (5/(24*60)
Which should give You needed result. I find this method more straightfoward and simpler to remember than using special functions.
If You want filter out data from last 12 hours than You can query it like this:
select * from mytable where TIME_STAMP <= current_timestamp - 0.5

Sql - find if date is between two dates

I have a field of FromDate and a field of ToDate.
I am looking for the rows that today is between the "from" and "to"
select * from job
where job.type='manager'
and '2014-01-22' between job.FromDate and job.ToDate
The query does not throw an exception , and it even returns some rows. But it isn't right- the rows it returns do not have the dates I am looking for.
P.S. the date format I am using is the correct one for my DB.
Try this
select * from job
where job.type='manager'
and job.FromDate <= '2014-01-22' and job.ToDate >= '2014-01-22'
Comparing dates is often tricky, especially if the values are stored as datetime and not date. The time components can affect the comparison. Another possibility is that ToDate is NULL for the most recent records. Here is one way to fix this:
select *
from job
where job.type ='manager' and
date('2014-01-22') between date(job.FromDate) and date(coalesce(job.ToDate, '2099-12-31'))
However, the use of the function on the columns can make the query less efficient. Instead, you might try:
select *
from job
where job.type ='manager' and
job.FromDate < '2014-01-23' and
(job.ToDate >= '2014-01-22' or job.ToDate is null);