You must Sign In to post a response.
  • Category: SQL Server

    Query for getting records older than 10 days from currentdate

    Hai,

    I need to get records which are more than or equal to 10 days older from a table.

    How to use the date function to retrieve the records.

    Please give suggestions

    with regards,
    Shyam
  • #750592
    Hello Shyam Kumar,

    I am confused about your requirement.
    What do you want exactly?

    select * from tableName where DateColumn < GETDATE() and DateColumn > getdate()-10

    it retrive all data between today date and 10 days older than today.
    I hope you get my point, if it is not as you want than i'll get back to you.

    Regards,
    Nirav Prabtani (Senior Web Developer)
    Email : niravjprabtani@gmail.com
    blog : niravprabtani.blogspot.in

  • #750593
    Hello Shyam,

    Please try the below query to get the records from sql server database where the records are older than 10 from today's date.

    select * from [table_name] where [DateColumn_name] > DATEADD(DAY,-10,getdate())

    Here DateColumn_name is the column name which is of type date.

    DATEADD is sqlserver built-in function which is going to Add the -10 days(DAY)(second parameter) to today's date(getdate()).

    Miss. Jain
    Microsoft Certified Technology Specialist in .Net

  • #750631
    Hi,


    If you want to know the 10 days before data then simply use dateadd function as Priya said using that you can achieve your goal refer below sample.



    Select * from tablename where col >= dateadd(d,-10,getdate());


    Hope this will help you to resolve your problem.

    --------------------------------------------------------------------------------
    Give respect to your work, Instead of trying to impress your boss.

    N@veen
    Blog : http://naveens-dotnet.blogspot.in/

  • #750663
    Hi,

    To select the data which are 10 days older, you can set the range of the dates as like below-

    select * from tblname where col1 < GETDATE() and DateColcol1umn > getdate()-10

    Thanks,
    Ashutosh Jha
    http://tricksroad.com

  • #750702
    Simply use the get date method as




    SELECT * from TabName

    WHERE DateColumn > dateadd(d,-10,getdate());

    Thanks & Regards
    Anil Kumar Pandey
    Microsoft MVP, DNS MVM

  • #750761
    Add this in to you WHERE clause:

    SELECT * FROM [Table Name]
    WHERE [Your Date Column] < DATEADD(day, -@NoOfDays, GETDATE())

    If your [Date Column] contains time stamp, Use below sql script

    SELECT * FROM [Table Name]
    WHERE [Your Date Column] < DateAdd(Day, DateDiff(Day, 0, GetDate()) - @NoOfDays, 0)


  • Sign In to post your comments