Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

check if  data is older than 24 hours

Posted on 2013-05-17
4
Medium Priority
?
2,376 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 200 total points
ID: 39175519
Try
Where datediff(hour,insert_time,getdate()) <24
0
 
LVL 93

Accepted Solution

by:
Patrick Matthews earned 1800 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 93

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 93

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

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

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

Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
Ready to get certified? Check out some courses that help you prepare for third-party exams.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

721 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