?
Solved

Compare Datetime Values in SQL

Posted on 2003-03-13
8
Medium Priority
?
460 Views
Last Modified: 2008-03-04
I need to compare two datetime values in my SQL database to determine which is the later time.

The following code is not working:

If objRS("LastUpdate") > request.form("FormStart") then
Response.write "This ticket has been updated by another user since the time you accessed it. You will need to check on the updated status of this ticket."
Response.End
End if

I knew this had to be too simple.  How would I compare these two datetime values?

Thanks.
0
Comment
Question by:Rougie
[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 Comments
 
LVL 4

Expert Comment

by:mberumen
ID: 8129663
use the datediff function...

0
 
LVL 4

Accepted Solution

by:
mberumen earned 400 total points
ID: 8129746
for example if you have two dates.. depending on how accurate you want to be you can get the difference in seconds, minutes, hours,days, etc

If (datediff("s",request.form("FormStart"),objRS("LastUpdate")) > 0 then  'form date is after recordset date
   ' add code here
else
   'add code here
end if
0
 

Author Comment

by:Rougie
ID: 8130475
The datediff function looks like it only compares whether or not the two datetime values are different, not whether or not one is actually later than the other.
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 3

Expert Comment

by:keystrokes
ID: 8130548
mberumen is right.  If the result is > 0 then request.form("FormStart") is smaller than objRS("LastUpdate"), and vis versus for a negative result.
0
 
LVL 4

Expert Comment

by:mberumen
ID: 8130560
it will return the difference between both dates in the specified unit.  so a positive value means that dateA is after dateB, whereas a negative value means that dateA is before DateB.

Perhaps I have  a typo on my code above, try this and check the results


Response.write "formdate - recorddate=" & datediff("s",request.form("FormStart"),objRS("LastUpdate"))

Response.write "recorddate - formDate=" & datediff("s",objRS("LastUpdate"),request.form("FormStart"))

You should see two different values (unless both dates are the same or they are dates only (no time part), in which case you need to use the days difference

Response.write "formdate - recorddate=" & datediff("d",request.form("FormStart"),objRS("LastUpdate"))

Response.write "recorddate - formDate=" & datediff("d",objRS("LastUpdate"),request.form("FormStart"))





0
 
LVL 10

Expert Comment

by:apollois
ID: 8130662
Hi Rougie,

>>>If objRS("LastUpdate") > request.form("FormStart") then

Simply convert the form data to date/time type:

If objRS("LastUpdate") > CDate(request.form("FormStart")) then


Of course, you really should check for a blank or non-date type:

strFormStart = request.form("FormStart")

IF VarType(strFormStart) = vbDate THEN

     If objRS("LastUpdate") > CDate(strFormStart) THEN
     '... remainder of your code here

ELSE

     '--- FORM INPUT ERROR ---
     'Display error msg, and redisplay FORM

END IF


Best Regards,
>apollois<
0
 

Author Comment

by:Rougie
ID: 8131400
You were right.  My confusion.  There's 50 extra points for you.
 
Thanks.
0
 
LVL 4

Expert Comment

by:mberumen
ID: 8131680
Thanks, glad to be able to help..

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

I recently decide that I needed a way to make my pages scream on the net.   While searching around how I can accomplish this I stumbled across a great article that stated "minimize the server requests." I got to thinking, hey, I use more than one…
I was asked about the differences between classic ASP and ASP.NET, so let me put them down here, for reference: Let's make the introductions... Classic ASP was launched by Microsoft in 1998 and dynamically generate web pages upon user interact…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Suggested Courses

765 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