Solved

Query records of current date minus 6 hours

Posted on 2007-04-04
17
657 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 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
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

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

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Ms Access VBA Variables 6 26
Foxpro errors 4 28
Cascading Combo boxes between 2 sub navigation forms 1 9
Filter a form 8 11
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Creating and Managing Databases with phpMyAdmin in cPanel.
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.
With Microsoft Access, learn how to start a database in different ways and produce different start-up actions allowing you to use a single database to perform multiple tasks. Specify a start-up form through options: Specify an Autoexec macro: Us…

816 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

8 Experts available now in Live!

Get 1:1 Help Now