Using dateadd

  • My select with the dateadd is as follows:

    select transactiondate from fuelinvoice where transactiondate=dateadd(d,-1,getdate())

    So it should bring me back stuff from yesterday's date.

    Does it have to do with the fact the getdate() function returns the time as well?

    Matt

  • Yes,

    If you want to match for the same day you'll have to convert each datetime component to a more general date:

    select transactiondate from fuelinvoice where convert(varchar(10),transactiondate,101)=convert(varchar(10),dateadd(d,-1,getdate()),101)

    or if you want to return all records that have been inserted in the last 24 hour period you could do the following:

    select transactiondate from fuelinvoice where transactiondate > dateadd(d,-1,getdate())

  • What does the 101 mean on the convert? I looked it up on books online but didnt see anything on 101.

    Matt

  • Try the following link. It's to BOL at the MSDN site, topic of CAST and CONVERT. This gives all the formats for CONVERT.

    http://msdn.microsoft.com/library/default.asp?url=/library/en-us/tsqlref/ts_ca-co_2f3o.asp

    K. Brian Kelley

    bkelley@sqlservercentral.com

    http://qa.sqlservercentral.com/columnists/bkelley/

    K. Brian Kelley
    @kbriankelley

  • It chooses the style for the date.

    If you look up information on the CONVERT function you should see information about style. 101 for example is mm/dd/yyyy.

Viewing 5 posts - 1 through 4 (of 4 total)

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