Date Math In SQL Server, Number of Days Elapsed Since Date In Table

I am fairly new to SQL Server, working in SQL Developer 14.

I have a requirement to pull records from a table after a specified number of days has passed.

I have a lot of experience in MS Access and I could accomplish this in an Access query using the Date Function.  'TableDate' is the field containing the date.  If I wantto make sure at least 30 days have passed:

Select .....
From ....
Where Date - TableDate > 30

Is there a way to do something similar in a SQL Select?
LVL 1
mlcktmguyAsked:
Who is Participating?
 
PortletPaulfreelancerCommented:
Always choose a method that does NOT require a function on the data. There are many reasons for this
 * in most cases it removes the ability to use an index on the column being amended (makes the query slower)
 * requires a function call on every row, quite unnecessarily

please choose this approach:

SELECT *
FROM YourtableName
WHERE TableDate > CAST((GETDATE() - 30) AS DATE)

notice that it doesn't alter the table data, instead it does just one calculation
2
 
Pawan KumarDatabase ExpertCommented:
Please try like this-

DateDiff function.

SELECT *
FROM YourtableName
WHERE DATEDIFF(d,TableDate,GETDATE()) > 30 

Open in new window


OR this

SELECT *
FROM YourtableName
WHERE TableDate > CAST((GETDATE() - 30) AS DATE)

Open in new window

0
 
mlcktmguyAuthor Commented:
Thanks for the comments and suggestions, they are very much appreciated.  I took some time off over the holiday so I have not had time to evaluate them yet.
1
 
mlcktmguyAuthor Commented:
Thank You
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.