Syntax for DateTime column WHERE clause

Posted on 2008-11-11
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,

Question by:rito1
    LVL 44

    Accepted Solution

    Hello rito1,

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

    Should do that.


    LVL 1

    Author Closing Comment

    Excellent, thanks.
    LVL 75

    Expert Comment

    by:Aneesh Retnakaran
    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)   )

    Featured Post

    Highfive Gives IT Their Time Back

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Join & Write a Comment

    Suggested Solutions

    JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
    The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
    This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
    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

    746 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

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now