none
SQL Statement for requesting date 60 days from date.today

    Question

  • Dear all

    How do i write a sql statement for selecting date 60 days away from todays date ?

     

    Select * from Database where Date <= 60 from date.today <-- something like that .

     

    Regards

    Noob2008

    Tuesday, February 08, 2011 2:18 AM

Answers

All replies

  • select * from yourTable where [Date] <= dateadd(day,-60, cast(getdate() as date)) -- assuming you're using SQL 2008
    

    This will select all older dates.

    If you need to select last 2 months, then change your condition this way

    select * from YourTable
    where [Date]>=dateadd(day,-60, cast(getdate() as date))
    


    Premature optimization is the root of all evil in programming. (c) by Donald Knuth

    Naomi Nosonovsky, Sr. Programmer-Analyst

    My blog
    Tuesday, February 08, 2011 2:22 AM
  • Or simple getdate()-60
    Best Regards, Uri Dimant SQL Server MVP http://dimantdatabasesolutions.blogspot.com/ http://sqlblog.com/blogs/uri_dimant/
    Tuesday, February 08, 2011 8:05 AM
  •         Dim oleConnection As OleDbConnection
            Dim oleCommand As OleDbCommand
            Dim dR As OleDbDataReader

            oleConnection = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:/Documents and Settings/MainDB.mdb;")   

         oleCommand = New OleDbCommand("Select [Date_expire] from Main where [Date_Renewed] <= dateadd(day,-60, cast(getdate()as date))", oleConnection)

            oleConnection.Open()

            dR = oleCommand.ExecuteReader()

     

    Hit the " Ierrorinfo.getdescription failed with E_FAIL(0x80004005) " Error . Is it my sqlstatement ?

    Tuesday, February 08, 2011 9:39 AM
  • You're using Access database and asking in SQL Server forum. I believe in Access to get the current date you need to use Now() function, but I haven't worked with Access for quite a while. I suggest to find an appropriate Access forum as here the answers are concentrated on SQL Server and they may not be applicable for Access.
    Premature optimization is the root of all evil in programming. (c) by Donald Knuth

    Naomi Nosonovsky, Sr. Programmer-Analyst

    My blog
    Tuesday, February 08, 2011 2:19 PM
  • In Access you can use the following as the condition:

    DateAdd("d",-60,Date())

    Dave

    Tuesday, February 08, 2011 4:40 PM
  • Access is my database , but i am using SQL statement in my VB window application. DOes it affect ?
    Wednesday, February 09, 2011 2:26 AM
  • You need to run a statement in the language Access can understand.
    Premature optimization is the root of all evil in programming. (c) by Donald Knuth

    Naomi Nosonovsky, Sr. Programmer-Analyst

    My blog
    • Marked as answer by Noob2008 Wednesday, February 09, 2011 4:43 AM
    Wednesday, February 09, 2011 3:28 AM
  • I finally got it . Although it i was coding in .net , but as my Database is Access, therefore the SQL i code in my .net application must be in a SQL language which my access database can understand :)

    Just to share

    here the finaly statement :)

    Select [Date_expire] from Main where [Date_Renewed] <= dateadd('d' ,60 ,date())

    Thanks ! NAOMI !

    :)

     

    Another good link to share

    http://sqlserver2000.databases.aspfaq.com/what-are-the-main-differences-between-access-and-sql-server.html

    • Marked as answer by Noob2008 Wednesday, February 09, 2011 9:59 AM
    Wednesday, February 09, 2011 4:47 AM
  • This is not an answer.
    Wednesday, September 11, 2013 7:34 AM