MS Access выбирает между двумя датами?

Я искал, но все результаты не помогли мне понять.

Мне нужно выбрать имена людей в возрасте 18-23 лет. Итак, моя попытка была:

WHERE ((People.Birth) Between (Now()-Year(18)) And (Now()-Year(23)))

Что я делаю неправильно? Решение как #some_date# - плохая идея!


person Vitali Kuzmin    schedule 11.02.2016    source источник
comment
Вы исследовали, что на самом деле возвращает Year(18)?   -  person Gord Thompson    schedule 11.02.2016
comment
См. datediff.   -  person user2864740    schedule 11.02.2016
comment
бля, столько раз понимал, что все проблемы надо решать после сна, а не после 25 часов дня. Year(18) возвращает 1900... 2 user2864740 - видел, но мне не помогло(   -  person Vitali Kuzmin    schedule 11.02.2016


Ответы (2)


Для правильного решения вам нужно использовать DateAdd и такую ​​функцию:

Public Function AgeSimple( _
  ByVal datDateOfBirth As Date) _
  As Integer

' Returns the difference in full years from datDateOfBirth to current date.
'
' Calculates correctly for:
'   leap years
'   dates of 29. February
'   date/time values with embedded time values
'
' DateAdd() is used for check for month end of February as it correctly
' returns Feb. 28. when adding a count of years to dates of Feb. 29.
' when the resulting year is a common year.
' After an idea of Markus G. Fischer.
'
' 2007-06-26. Cactus Data ApS, CPH.

  Dim datToday  As Date
  Dim intAge    As Integer
  Dim intYears  As Integer

  datToday = Date
  ' Find difference in calendar years.
  intYears = DateDiff("yyyy", datDateOfBirth, datToday)
  If intYears > 0 Then
    ' Decrease by 1 if current date is earlier than birthday of current year
    ' using DateDiff to ignore a time portion of datDateOfBirth.
    intAge = intYears - Abs(DateDiff("d", datToday, DateAdd("yyyy", intYears, datDateOfBirth)) > 0)
  End If

  AgeSimple = intAge

End Function

Тогда ваш запрос будет содержать предложение where:

WHERE AgeSimple(People.Birth) Between 18 And 23
person Gustav    schedule 11.02.2016

Наконец решил это простым

WHERE ((People.Birth) Between (Now()- 365*18) And (Now()-365*23))

Если у вас есть лучшее решение - добро пожаловать

person Vitali Kuzmin    schedule 11.02.2016