Solved

check if  data is older than 24 hours

Posted on 2013-05-17
4
2,085 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
[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
  • 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

Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

Question has a verified solution.

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

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.
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Viewers will learn how the fundamental information of how to create a table.

729 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