Dates in query

  • I'm basically trying to return rows in a given timeframe. I am using Now() as the start date limit, and I want to set the end date to Now() + 30 days. Can anyone help me with that function call?

  • Last time I looked "Now()" wasn't an SQL Server function. Are you doing this in code?

    For SQL Server you would have either,

    SELECT GetDate()-30
    SELECT DATEADD(dd, -30, GetDate())

     

    --------------------
    Colt 45 - the original point and click interface

  • Last time I looked "Now()" wasn't an SQL Server function

    Gee, and I always wondered wtf it didn't work

     

    Chris,

    if your date data contains a time portion unlike midnight, you might run into a pitfall here. In this case you might be better off using something like

    WHERE

     column >= DATEADD(d,0,DATEDIFF(d,0,@dt))

    AND

     column <= DATEADD(d,30,DATEDIFF(d,0,@dt))

    In case your looking always 1 month ahead, you can use this instead

     column <= DATEADD(m,1,DATEDIFF(d,0,@dt))

    This automatically deals with the varying # of days in the months.

    Last, but not least, a shameless plug here:

    http://www.sql-server-performance.com/fk_datetime.asp

    --
    Frank Kalis
    Microsoft SQL Server MVP
    Webmaster: http://www.insidesql.org/blogs
    My blog: http://www.insidesql.org/blogs/frankkalis/[/url]

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

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