Results 1 to 2 of 2
  1. #1
    Join Date
    May 2010
    Posts
    2

    Unanswered: Query Help - Selecting between two date ranges

    Hi All,

    I am trying to count rows in a table where a datetime field entry is between todays date and the previous 14 days.

    Code:
    SELECT COUNT(DebtId)
    FROM tblFees
    WHERE DateInvoiced between dateadd(day,datediff(day,0,getdate())-0,0) and dateadd(day,datediff(day,0,getdate())-14,0)
    This doesn't return any results, I am not sure if I am using the correct date format?


    I am also trying to do a SUM on a field within the same table but when I attempt to select both a COUNT and a SUM within the same query I get the following error:

    Code:
    Only one expression can be specified in the select list when the subquery is not introduced with EXISTS.

    Sorry if this is a basic question or has been asked before, I'm new to SQL and would really appreciate the guidance.

    Many Thanks

    Matt

  2. #2
    Join Date
    Feb 2004
    Location
    One Flump in One Place
    Posts
    14,912
    if you run this you'll see your formulae are fine, but you have the dates in the wrong order for the BETWEEN arguments.
    Code:
    SELECT  dateadd(day,datediff(day,0,getdate())-0,0)
          , dateadd(day,datediff(day,0,getdate())-14,0)
    I can't help with the second problem without the SQL.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •