sum() function - need help

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • arvindpraj
    New Member
    • Oct 2006
    • 2

    sum() function - need help

    Hi,

    When i used sum() function in the SQL queries in MS Access for calculating the age of people in a database. i can able to get the result but the result comes out with a minus (negative) sign in front of it..
    I had posted my SQL query below

    select sum(year(now())-year([date_of_birth])<30) as Less_than_30, sum(year(now())-year([date_of_birth])>=30) as 30_and_more from students

    Can anyone please explain about this and suggest a solution..

    Thank You

    Arvind
  • Tanis
    New Member
    • Mar 2006
    • 143

    #2
    Try this function instead.

    Public Function MyAge(dteDOB As Date, Optional SpecDate As Variant) As Integer
    Dim dteBase As Date, intCurrent As Date, intEstAge As Integer
    If IsMissing(SpecD ate) Then
    dteBase = Date
    Else
    dteBase = SpecDate
    End If
    intEstAge = DateDiff("yyyy" , dteDOB, dteBase)
    intCurrent = DateSerial(Year (dteBase), Month(dteDOB), Day(dteDOB))
    MyAge = intEstAge + (dteBase < intCurrent)
    End Function

    Call it in a query like so

    MyAge([YourDateField])

    Comment

    • arvindpraj
      New Member
      • Oct 2006
      • 2

      #3
      Originally posted by Tanis
      Try this function instead.

      Public Function MyAge(dteDOB As Date, Optional SpecDate As Variant) As Integer
      Dim dteBase As Date, intCurrent As Date, intEstAge As Integer
      If IsMissing(SpecD ate) Then
      dteBase = Date
      Else
      dteBase = SpecDate
      End If
      intEstAge = DateDiff("yyyy" , dteDOB, dteBase)
      intCurrent = DateSerial(Year (dteBase), Month(dteDOB), Day(dteDOB))
      MyAge = intEstAge + (dteBase < intCurrent)
      End Function

      Call it in a query like so

      MyAge([YourDateField])

      Thank You

      Arvind

      Comment

      Working...