Solved

check if  data is older than 24 hours

Posted on 2013-05-17
4
1,882 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
ID: 39175519
Try
Where datediff(hour,insert_time,getdate()) <24
0
 
LVL 92

Accepted Solution

by:
Patrick Matthews earned 450 total points
ID: 39175524
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
ID: 39175531
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
ID: 39175553
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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Viewers will learn how the fundamental information of how to create a table.

820 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