Solved

check if  data is older than 24 hours

Posted on 2013-05-17
4
1,640 Views
Last Modified: 2013-05-22
hi guys

i have a table customer that has following columns

customer-ID  insert_time
100                2013-05-17 8:17:32.197
200                2013-05-15 18:17:32.197


how can i write a sql which will bring me data older than a day
I am trying to do
( CONVERT (date, SYSDATETIME()) - cast(insert_time AS Date) ) > 1


but get error

Operand data type date is invalid for subtract operator.


any ideas appreaited
thanks
0
Comment
Question by:royjayd
  • 3
4 Comments
 
LVL 7

Assisted Solution

by:Ross Turner
Ross Turner earned 50 total points
Comment Utility
Try
Where datediff(hour,insert_time,getdate()) <24
0
 
LVL 92

Accepted Solution

by:
Patrick Matthews earned 450 total points
Comment Utility
If you mean "only return it if insert_time was more than 24 hours ago":

SELECT customer_ID, insert_time
FROM customer
WHERE insert_time < DATEADD(hour, -24, GETDATE())

Open in new window

0
 
LVL 92

Expert Comment

by:Patrick Matthews
Comment Utility
If you really mean, "older than yesterday"...

SELECT customer_ID, insert_time
FROM customer
WHERE insert_time < DATEADD(day, DATEDIFF(day, 0, GETDATE()) - 1, 0)

Open in new window

0
 
LVL 92

Expert Comment

by:Patrick Matthews
Comment Utility
Try
Where datediff(hour,insert_time,getdate()) <24

Be careful with DATEDIFF.  DATEDIFF does NOT measure elapsed time.  Rather, it measures time period boundaries.

Consider this expression:

DATEDIFF(hour, '2013-05-01 00:59:59', '2013-05-02')

That will return 24, even the true elapsed time is 23:00:01.

The smaller the interval you use with DATEDIFF, the smaller the possible error--for example, using seconds you would never be off by more than a second--but there is still the possibility of error.
0

Featured Post

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Join & Write a Comment

Suggested Solutions

Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

763 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now