Syntax for DateTime column WHERE clause

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
LVL 1
rito1Asked:
Who is Participating?
 
TimCotteeConnect With a Mentor Head of Software ServicesCommented:
Hello rito1,

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

Should do that.

Regards,

TimCottee
0
 
rito1Author Commented:
Excellent, thanks.
0
 
Aneesh RetnakaranDatabase AdministratorCommented:
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
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.