Solved

Query records of current date minus 6 hours

Posted on 2007-04-04
17
632 Views
Last Modified: 2008-02-01
Microsoft access. From a link table I need a query that selects all the fields for a table called DATA, however this has to select records from the current date minus 6 hours, but the field DATE is setup as date
anyway to this this
0
Comment
Question by:teogos
  • 8
  • 6
  • 3
17 Comments
 
LVL 119

Expert Comment

by:Rey Obrero
ID: 18853404
does the field DATE have hours on it?

select *
from [Data]
where [Date] <=Dateadd("h",-6,Now())
0
 

Author Comment

by:teogos
ID: 18853417
No hours just date
0
 
LVL 119

Expert Comment

by:Rey Obrero
ID: 18853432
then, you can not get anything based on your criteria.
0
 
LVL 119

Expert Comment

by:Rey Obrero
ID: 18853444
you may have to change the default format to  General Date
0
 

Author Comment

by:teogos
ID: 18853481
This Link Table comes from a SQL table setup as Datetime
0
 

Author Comment

by:teogos
ID: 18853736
Well, since this cannot be done on Access, now let ask you this  with the following code what I want to accomplish works fine, but how can get this into a SQL query, I run this on the query analizer and work fine, but how do I convert to a procedure

DECLARE @startdate datetime
IF datepart(hour, getdate()) < 6
     SET              @startdate = CAST(CONVERT(CHAR(10), getDate() - 1, 121) AS DateTime)
ELSE
     SET              @startdate = CAST(CONVERT(CHAR(10), getDate(), 121)  AS DateTime)

select *, @startdate
from DATA
WHERE YEAR(Date) = YEAR(@startdate) and month(date) = month(@startdate) and day(date) = day(@startdate)
0
 
LVL 23

Expert Comment

by:Christopher Kile
ID: 18853948
You're confusing me.  Your test query checks to see if the current time is less than 6 AM.  Why aren't you using DateDiff()? Versions exist in both Access and SQL Server.

In the following examples, I'm going to call your date field theDate to avoid keyword collision.

Do you want to match a date which is at least six hours previous to a given date?

SELECT * FROM DATA
WHERE DateDiff("h", theDate,  Now()) <= 6

0
 

Author Comment

by:teogos
ID: 18856834
Where I want is to show records after midnight that from the same date example today is 4/5/2007
however after midnight when I run the query, Even so the system date is 4/6/2007 I want to show records from the 4/5/2007. Then after 6:00 AM on the 4/6/2007 I am back to normal date or current
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 23

Expert Comment

by:Christopher Kile
ID: 18858069
In Access, this would be:

SELECT * FROM DATA
WHERE
Format("yyyymmdd", theDate) =
Format("yyyymmdd", iif(datepart("h", Now()) < 6, DateAdd("d", -1, Date), Date()))

Since your original request was for an Access solution, perhaps this will do?
0
 

Author Comment

by:teogos
ID: 18859370
no does not work it brings all the records from previous days, I just want to see today's
0
 
LVL 119

Expert Comment

by:Rey Obrero
ID: 18859477
teogos,
if you don't have a time value from the fieldDate, you will not get the correct records.
0
 
LVL 23

Expert Comment

by:Christopher Kile
ID: 18859953
Run the following query against your database:

SELECT
theDate 'Substitute your field name here
,
CDate(Format("yyyymmdd", theDate))
,
CDate(Format("yyyymmdd", iif(datepart("h", Now()) < 6, DateAdd("d", -1, Date), Date())))
FROM
Data
Order by theDate DESC

You should be able to see immediately why the dates don't match.  If you have trouble fixing it or understanding what you're seeing, please send me some samples.  Thanks in advance.
0
 

Author Comment

by:teogos
ID: 18860669
Date column shows dates in desc order  
column 2   shows #Error
column 2  shows #Error
0
 

Author Comment

by:teogos
ID: 18860859
Anything you guys can help me. If I have to a query on the SQL or VBA for access, I am increasing the points , the only time I am looking for is after midnight to see the records for today's
0
 
LVL 119

Expert Comment

by:Rey Obrero
ID: 18861199

try this

select *
from Data
where ((Data.FieldDate)=iif(timevalue(now)<= timeSerial(6,0,0), Date()-1,Date()));

0
 
LVL 119

Accepted Solution

by:
Rey Obrero earned 500 total points
ID: 18861371
or

select *
from Data
where ((Data.FieldDate)=iif(timevalue(now)>= timeSerial(0,0,0) And timevalue(now)<= timeSerial(6,0,0), Date()-1,Date()));
0
 

Author Comment

by:teogos
ID: 18872739
Thanks
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

APEX (Application Express) is used to develop a web application from Oracle. SQL Workshop is one of the tools that comes with Oracle APEX to query or modify the database objects or to make any changes to the structure.
CCModeler offers a way to enter basic information like entities, attributes and relationships and export them as yEd or erviz diagram. It also can import existing Access or SQL Server tables with relationships.
Familiarize people with the process of utilizing SQL Server stored procedures from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Micr…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now