Re: Date calculation

From: Hugo Kornelis (hugo_at_pe_NO_rFact.in_SPAM_fo)
Date: 08/03/04

  • Next message: Dave Wickert [MSFT]: "Re: Connect from sql query via IP adress - is i possible?"
    Date: Tue, 03 Aug 2004 21:59:59 +0200
    
    

    On Tue, 3 Aug 2004 10:37:13 -0700, Bob wrote:

    >Im relatively new with sql and im having a problem
    >creating a criteria based on todays date less 150 days.
    >
    >I tried to use the following...
    > < GetDate () -150 however it will not let me use this.
    >
    >I know that there must be away to do this, but I need
    >help.. Thank you in advance

    Hi Bob,

    Try it with DATEADD(day, -150, getdate()).

    If you remove the space between getdate and (), your code would work as
    well, but I recommend using DATEADD anyway - just subtracting 150 relies
    on the internal storage of datetime values; this is not guaranteed to
    remain unchanged between SQL Server versions.

    Also remember that getdate() gives current date *AND* time. Not a problem
    for "smaller than" comparissons, but if you ever have to match on "equal"
    date, you'll have to use some extra tricks to get rid of the time portion.

    Best, Hugo

    -- 
    (Remove _NO_ and _SPAM_ to get my e-mail address)
    

  • Next message: Dave Wickert [MSFT]: "Re: Connect from sql query via IP adress - is i possible?"

    Relevant Pages

    • Date calculation
      ... Im relatively new with sql and im having a problem ... creating a criteria based on todays date less 150 days. ... < GetDate -150 however it will not let me use this. ...
      (microsoft.public.sqlserver.mseq)
    • Re: Dynamic date selection
      ... That's hard to say without knowing how your dates are stored ... or what you need to do with the time portion and how that ... GetDate(), 101) as Datetime)) ... I've tried combinations of DateAdd and GetDateetc, ...
      (microsoft.public.sqlserver.dts)
    • getDate() without time
      ... I want to be able to always put in only the date in my datetime field. ... This causes me a problem with my testing for todays date or any other dates ... How would I deal with the getdate() issue for the default? ... Tom. ...
      (microsoft.public.sqlserver.programming)
    • Between Dates
      ... This way I don't have to worry about the time portion ... which is stored as the decimal portion of the date/time. ... >I have the following criteria in my query on a date.. ...
      (microsoft.public.access.queries)
    • Re: Default values
      ... CREATE TABLE foo(dt SMALLDATETIME DEFAULT GETDATE()) ... Note that a time portion will still be stored, ... "Jeff Cichocki" wrote in message ...
      (microsoft.public.sqlserver.server)