how to pass date dynamically

  • Hi everyone..

    I have a query that gets the 30 days back information information

    for example:select * from table name where date=getdate-30

    I want to pass the valu dynamically place of 30.how to solve this can any one give the solution

    Regards ..

    Anil...

    Regards..
    guru12

  • may be it helps you :

    declare @value int

    declare @sql varchar(200)

    set @sql= 'select * from tablename where date=getdate()-'+@value+

    exec(@sql)

    Hi everyone..

    I have a query that gets the 30 days back information information

    for example:select * from table name where date=getdate-30

    I want to pass the valu dynamically place of 30.how to solve this can any one give the solution

    Regards ..

    Anil...

  • Use a variable for your value of -30.

    Employ the function DATEADD()

    Take great care with DATETIME data types with regard to the time portion.

    Ignore pointless recommendations to use dynamic SQL.

    “Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

    For fast, accurate and documented assistance in answering your questions, please read this article.
    Understanding and using APPLY, (I) and (II) Paul White
    Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden

Viewing 3 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply