472,958 Members | 2,276 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,958 software developers and data experts.

Problems using datediff in SQL with ASP

I'm trying to do something that should be trivial. I'm a teacher at an elementary school, and I'm setting up an ASP form page for teachers to vote each afternoon on which dismissal line behaved best.

I've done this sort of thing before. For example, I've set up a form page teachers use each day to record lunch counts. The results are saved to a database on the website, and a second ASP page displays the result using this "custom query":

SELECT *
FROM Results
WHERE DatePart('y',Results.Timestamp)=DatePart('y',Now() )
ORDER BY teacherName ASC,Timestamp ASC;

That works fine and returns only records made today. For this new form, however, I need records returned within the last 16 hours or so. For this I've tried to use the datediff function, but I can't get it to work. I've tried:

SELECT *
FROM Results
WHERE DATEDIFF('Hour',Results.Timestamp,Now())<=16
ORDER BY line ASC,Timestamp ASC;

Instead of 'Hour' I've tried "Hour", Hour, 'hh', 'hh", and hh. I've tried getdate() instead of Now().

I know the results page is properly connected with the database because I get results if I don't try using a "custom query". I'm using FrontPage 2003. Thanks in advance.
Oct 16 '07 #1
2 3365
try this:

SELECT *
FROM Results
WHERE DATEDIFF(hh,Timestamp,GetDate())<=16
ORDER BY line ASC,Timestamp ASC;
Oct 16 '07 #2
markrawlingson
346 Expert 100+
Expand|Select|Wrap|Line Numbers
  1. SELECT *
  2. FROM Results
  3. WHERE DATEDIFF(hh,Result.Timestamp,GetDate())<=16
  4. ORDER BY line ASC,Timestamp ASC;
  5.  
Is the correct way to do it for what you want to do.

But, it depends on what the value of Result.Timestamp is. I'm assuming this is a column in your database that gets updated with the date and time when a record is created, but in what format? Is it just the time, the time and date, just the date, the date in dd/mm/yyyy format, the date in mm/dd/yyyy format, the date in dd/mm/yy format, the date in mm/dd/yy format etc etc?? There are many combinations, and each will return a different result.

Your problem is most likely that Result.Timestamp is in a different format than the format in which GetDate() is returned. If this is the case, it will give you an inaccurate number of hours returned, probably much greated than 16, which means the recordset will return no records, or in some cases may even throw an error.

An overview of DateDiff() and exactly HOW it computes the difference in dates

DateDiff computes the difference in dates by FIRST translating both the Start Date and the End Date into an integer representing the number of (minutes, hours, days, years, minutes, milliseconds, etc) since the epoch (00:00:00 GMT, January 1, 1970;), and THEN comparing them.
Oct 16 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Mike | last post by:
Hi all, I am writing a database using SQL as the back end and Access as the front end. My project is to create a rota for when people have to work. The problem I have is setting up the tables...
19
by: Lauren Quantrell | last post by:
I have a stored procedure using Convert where the exact same Convert string works in the SELECT portion of the procedure but fails in the WHERE portion. The entire SP is listed below....
2
by: CDF | last post by:
In one of my tables, I have a field that has time durations in it. The field is currently in text format. Is there a way that I can convert this field into a format where I can SUM the durations? ...
1
by: intl04 | last post by:
I'm trying to set up a query that will include a new field ('Days until completion') whose value is derived from the DateDiff function. I think I have the syntax correct but am not sure. Days...
2
by: Contro | last post by:
Hi there! I hope you can help me! I have the following code: SELECT COUNT(Applications.) AS TOTAL FROM Applications WHERE DateDiff('d',,Date()) <6 AND = "Certificate"; Which returns the...
4
by: John Ortt | last post by:
Hi Everyone, hope you can help. Our company has recently migrated from NT and Access 97 to XP and Access 2003 and in the process a number of problems have surfaced. The team I work in are...
7
by: Adrian | last post by:
I hit on this problem converting a VB.NET insurance application to C#. Age next birthday calculated from date of birth is often needed in insurance premium calculations. Originally done using...
3
by: Price Brattin | last post by:
Why is the DateDiff function in the following code returning zero? Dim FileDate, TransmissionDate as Date Dim TranDay, FileDay, DayDiff as Inteter TransmissionDate = #2/5/2006 1:57:56 PM#...
1
by: Sultan Ali Alhassni | last post by:
I was trying to use the datediff function in the Update query and I added the following on the upate to field : Datediff("d", , where Sentdate: is a date stored in a table called cycle_table ...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
2
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.