MSAccess: Return query for specific date from datetime value - ms-access

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.

Related

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 query between two dates and two times

I would like to query a mySQL table to pull out data between two dates and two times. I know how to do this for a single "datetime" column using the "between" call but my columns are one "date" column, and one "time" column. All the solution I can find online are for single datetime columns.
My ranges go from "day1" at 15:30 to day1+1day at 15:14
So far I can get the following range (which works):
SELECT time,
close
FROM intraday_values
WHERE date="2005-03-01"
and time between "15:30" and "23:59"
But I obviously need to incorporate 2 dates and two times. I have tried the following but get an error:
SELECT time,
close
FROM intraday_values
between date="2005-03-01"
and time="15:30"
and date="2005-03-02"
and time = "15:14"
Could someone help me formulate the query correctly? Many thanks
Not sure if your date field is indexed. If they are then the "concat" examples others have given may not perform very well.
As an alternative you can use a query of the form:
select *
from foo
where (date > lower_date and date < upper_date) -- technically this clause isn't needed if they are a day apart
or (date = lower_date and time >= lower_time)
or (date = upper_date and time <= upper_time)
It's not pretty but it works and will allow mysql to make use of indexes on the date field if they exist.
So your query would be
SELECT time,
close
FROM intraday_values
where (date > "2005-03-01" and date < "2005-03-02")
or (date = "2005-03-01" and time >= "15:30")
or (date = "2005-03-02" and time <= "15:14")
Use concat to combine these two column and cast it to datetime for the best results
SELECT
time,close
FROM
intraday_values
where
cast(concat(date," ",time) as datetime)
between
cast("2005-03-01 15:30") as datetime
and cast("2005-03-02 15:14") as datetime

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);

MySQL Return Today's Records Using DATE from DATETIME Field

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);

mysql search by time on time stamp

I am stuck with this issue for a while now any solutions are welcomed.
I have a table in MySQL and i am storing a timestamp.
Now lets say if i search on the basis of only date it works.
SELECT *
FROM table
WHERE timestamp > "2012-03-12";
this fetches all data where timestamp value is greater than 2012-03-12 00:00:00.
what i want is a query like this :
SELECT *
FROM table
WHERE timestamp > "10:20:09";
where all records with tie greater than 10:20:09 is fetched irrespective of the date.
Use this statement (it will get the time from your datetime column and compare it with the time)
SELECT *
FROM table
WHERE DATE_FORMAT(yourDatecolumn, '%H:%i:s') > '10:20:00'
Warning
This time format is for EU times (where 1pm = 13), if you want US format, use '%h:%i:s'