[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Syntax for DateTime column WHERE clause

Posted on 2008-11-11
3
Medium Priority
?
685 Views
Last Modified: 2012-05-05
Hi All,

I have a table with a datetime column and I need help querying this column to show records within the past 30 days.

Please could anyone share with me the syntax to achieve this.

Many thanks,

Rit
0
Comment
Question by:rito1
3 Comments
 
LVL 43

Accepted Solution

by:
TimCottee earned 2000 total points
ID: 22929488
Hello rito1,

Select * From yTable Where DateDiff(day,MyDateTimeColumn,GetDate()) <= 30

Should do that.

Regards,

TimCottee
0
 
LVL 1

Author Closing Comment

by:rito1
ID: 31515473
Excellent, thanks.
0
 
LVL 75

Expert Comment

by:Aneesh Retnakaran
ID: 22929513
if you have an index on DateColumn ,this will give you better results

Select * From yTable Where DateColumn >= dateadd(dd, -30, convert(varchar,getdate(),112)   )
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

When trying to connect from SSMS v17.x to a SQL Server Integration Services 2016 instance or previous version, you get the error “Connecting to the Integration Services service on the computer failed with the following error: 'The specified service …
This month, Experts Exchange sat down with resident SQL expert, Jim Horn, for an in-depth look into the makings of a successful career in SQL.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

830 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