Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 Help with DateDiff? DateAdd maybe?

Author  Topic 

toddhd
Yak Posting Veteran

55 Posts

Posted - 2006-09-15 : 16:02:08
I need to determine if 30 minutes has passed since the last time something was logged. So I basically want to do:

SELECT * from MyTable
WHERE LastLogDate = 30 minutes or more since the current date/time

I'm pretty sure that DateDiff is what I'm looking for, but I can't figure it out. Please help! We are in crunch mode, and this is a security issue I'm tasked with. Thank you kindly in advance.

-Todd Davis

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2006-09-15 : 16:05:27
SELECT * from MyTable
WHERE LastLogDate BETWEEN DATEADD(mi, -30, GETDATE()) AND GETDATE()

Tara Kizer
Go to Top of Page

toddhd
Yak Posting Veteran

55 Posts

Posted - 2006-09-15 : 17:53:14
Thank you!

-Todd Davis
Go to Top of Page

Kristen
Test

22859 Posts

Posted - 2006-09-16 : 01:32:11
If you are not familiar with the function you might want to use the full name in the first parameter - there are options for mm, m, mi, n and ms all covering Month, Minute and Millisecond - so someone reading the code would be forgiven for wrongly interpreting it, and for that reason I personally would use:

DATEADD(Minute, -30, GETDATE())

There are other potential howlers too : yyyy, yy, y - one of those is NOT year and dd, d, dy - one of those is NOT day!

Kristen
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2006-09-16 : 08:49:26
>>If you are not familiar with the function you might want to use the full name in the first parameter

I always prefer to use full name for better clarity

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2006-09-19 : 14:26:26
Heck no! Shortest code possible. If any junior developer can't figure something out, they should have been trained to look it up in BOL. Do you type CREATE PROCEDURE or CREATE PROC? T-SQL code is no different than other programming languages when it comes to this type of thing. Why do the full names in T-SQL when you almost certainly don't in <pick your favorite programming language>.

Tara Kizer
Go to Top of Page

Kristen
Test

22859 Posts

Posted - 2006-09-19 : 15:12:00
"CREATE PROCEDURE or CREATE PROC"

Actually I type CREATE PROCEDURE - but I'd be just as happy with CREATE PROC.

But I feel very strongly about this particular one because the parameters for the date functions are obtuse:

mm, m, mi, n, ms = Month / Minute / Millisecond
yyyy, yy, y = Year / dayOfYear
dd, d, dy = Day / dayofyear

and I have no wish to sort out the muddle caused by someone thinking they knew the purpose, and getting it wrong.

"-SQL code is no different than other programming languages ... "

Indeed, and that causes me to adopt a "defensive programming" stance

Kristen
Go to Top of Page

rockmoose
SQL Natt Alfen

3279 Posts

Posted - 2006-09-19 : 15:39:20
This is shorter, and adopts a defensive stance
SELECT * from MyTable
WHERE LastLogDate >= DATEADD(minute, -30, GETDATE())


rockmoose
Go to Top of Page
   

- Advertisement -