473,396 Members | 1,849 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 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 3404
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 ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.