?
Solved

Query records of current date minus 6 hours

Posted on 2007-04-04
17
Medium Priority
?
700 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
Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

 
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

Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

Question has a verified solution.

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

The Windows Phone Theme Colours is a tight, powerful, and well balanced palette. This tiny Access application makes it a snap to select and pick a value. And it doubles as an intro to implementing WithEvents, one of Access' hidden gems.
Recently, Microsoft released a best-practice guide for securing Active Directory. It's a whopping 300+ pages long. Those of us tasked with securing our company’s databases and systems would, ideally, have time to devote to learning the ins and outs…
What’s inside an Access Desktop Database. Will look at the basic interface, Navigation Pane (Database Container), Tables, Queries, Forms, Report, Macro’s, and VBA code.
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Suggested Courses

770 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