Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Query records of current date minus 6 hours

Posted on 2007-04-04
17
Medium Priority
?
708 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 8
  • 6
  • 3
17 Comments
 
LVL 120

Expert Comment

by:Rey Obrero (Capricorn1)
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 120

Expert Comment

by:Rey Obrero (Capricorn1)
ID: 18853432
then, you can not get anything based on your criteria.
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 120

Expert Comment

by:Rey Obrero (Capricorn1)
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
 
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 120

Expert Comment

by:Rey Obrero (Capricorn1)
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 120

Expert Comment

by:Rey Obrero (Capricorn1)
ID: 18861199

try this

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

0
 
LVL 120

Accepted Solution

by:
Rey Obrero (Capricorn1) earned 2000 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Microsoft Access is a place to store data within tables and represent this stored data using multiple database objects such as in form of macros, forms, reports, etc. After a MS Access database is created there is need to improve the performance and…
We live in a world of interfaces like the one in the title picture. VBA also allows to use interfaces which offers a lot of possibilities. This article describes how to use interfaces in VBA and how to work around their bugs.
In Microsoft Access, learn different ways of passing a string value within a string argument. Also learn what a “Type Mis-match” error is about.
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…

618 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