datediff data mismatch

I have a query with a date diff in years, when I try to filter the row with >= 18 I get a type mismatch error.  How can I get around this
smurfer69Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Rey Obrero (Capricorn1)Commented:
check for Null values of the fields involve in your dateDiff function
0
smurfer69Author Commented:
I did, I filtered out any null values for both entries.
0
Rey Obrero (Capricorn1)Commented:
post the sql of the query you are using
0
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

smurfer69Author Commented:
It is kind of long, and where I am running into the problem is when I base a query on this query.


SELECT tblCardiacCathLab.PatientDateOfBirth, tblCardiacCathLab.[Date of Procedure], tblCardiacCathLab.PatientLastName, tblCardiacCathLab.PatientFirstName, tblCardiacCathLab.PatientMedicalRecordNumber, Format([PatientMedicalRecordNumber],"00000000") AS mrn, CDate([PatientDateOfBirth]) AS dob, DateDiff('yyyy',[dob],[date Of procedure]) AS age, tblCardiacCathLab.Attending, tblCardiacCathLab.RHC, tblCardiacCathLab.[R/LHC], tblCardiacCathLab.LHC, tblCardiacCathLab.BX, tblCardiacCathLab.IVUS, tblCardiacCathLab.[PTCA(balloon)], tblCardiacCathLab.Stent, tblCardiacCathLab.ICD, ([mrn] & [date of procedure]) AS filter
FROM tblCardiacCathLab
WHERE ((Not (tblCardiacCathLab.PatientDateOfBirth) Is Null) AND (Not (tblCardiacCathLab.[Date of Procedure]) Is Null) AND ((tblCardiacCathLab.RHC)=Yes)) OR ((Not (tblCardiacCathLab.PatientDateOfBirth) Is Null) AND (Not (tblCardiacCathLab.[Date of Procedure]) Is Null) AND ((tblCardiacCathLab.[R/LHC])=Yes)) OR ((Not (tblCardiacCathLab.PatientDateOfBirth) Is Null) AND (Not (tblCardiacCathLab.[Date of Procedure]) Is Null) AND ((tblCardiacCathLab.LHC)=Yes)) OR ((Not (tblCardiacCathLab.PatientDateOfBirth) Is Null) AND (Not (tblCardiacCathLab.[Date of Procedure]) Is Null) AND ((tblCardiacCathLab.BX)=Yes)) OR ((Not (tblCardiacCathLab.PatientDateOfBirth) Is Null) AND (Not (tblCardiacCathLab.[Date of Procedure]) Is Null) AND ((tblCardiacCathLab.IVUS)=Yes)) OR ((Not (tblCardiacCathLab.PatientDateOfBirth) Is Null) AND (Not (tblCardiacCathLab.[Date of Procedure]) Is Null) AND ((tblCardiacCathLab.[PTCA(balloon)])=Yes)) OR ((Not (tblCardiacCathLab.PatientDateOfBirth) Is Null) AND (Not (tblCardiacCathLab.[Date of Procedure]) Is Null) AND ((tblCardiacCathLab.Stent)=Yes))
ORDER BY tblCardiacCathLab.[Date of Procedure] DESC;
0
Rey Obrero (Capricorn1)Commented:
try this query

SELECT tblCardiacCathLab.PatientLastName, tblCardiacCathLab.PatientDateOfBirth, tblCardiacCathLab.[Date of Procedure], DateDiff('yyyy',[PatientDateOfBirth],[Date of Procedure]) as Age
From tblCardiacCathLab
Where tblCardiacCathLab.PatientDateOfBirth <> Null And tblCardiacCathLab.[Date of Procedure] <>Null
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Gustav BrockCIOCommented:
You will need something like this:
SELECT 
  tblCardiacCathLab.PatientDateOfBirth, 
  tblCardiacCathLab.[Date of Procedure], 
  tblCardiacCathLab.PatientLastName, 
  tblCardiacCathLab.PatientFirstName, 
  tblCardiacCathLab.PatientMedicalRecordNumber, 
  Format([PatientMedicalRecordNumber],"00000000") AS mrn, 
  CDate([PatientDateOfBirth]) AS dob, 
  DateDiff('yyyy',[dob],[date Of procedure]) AS Age, 
  tblCardiacCathLab.Attending, 
  tblCardiacCathLab.RHC, 
  tblCardiacCathLab.[R/LHC], 
  tblCardiacCathLab.LHC, 
  tblCardiacCathLab.BX, 
  tblCardiacCathLab.IVUS, 
  tblCardiacCathLab.[PTCA(balloon)], 
  tblCardiacCathLab.Stent, 
  tblCardiacCathLab.ICD, 
  ([mrn] & [date of procedure]) AS filter
FROM 
  tblCardiacCathLab
WHERE 
  tblCardiacCathLab.PatientDateOfBirth Is Not Null
  AND 
  tblCardiacCathLab.[Date of Procedure] Is Not Null
  AND 
  tblCardiacCathLab.RHC=True
  AND
  tblCardiacCathLab.[R/LHC]=True
  AND
  tblCardiacCathLab.LHC=True
  AND 
  tblCardiacCathLab.BX=True
  AND
  tblCardiacCathLab.IVUS=True
  AND
  tblCardiacCathLab.[PTCA(balloon)]=True
  AND
  tblCardiacCathLab.Stent=True
ORDER BY 
  tblCardiacCathLab.[Date of Procedure] DESC;

Open in new window


Further, as DateDiff just returns the difference in calender years, you need a proper function to calculate age like the one below. Then adjust the age expression of your query to:

  Years([dob],[date Of procedure]) AS Age,

Public Function Years( _
  ByVal datDOB As Date, _
  ByVal datNow As Date, _
  Optional ByVal booLinear As Boolean) _
  As Integer

' Returns the difference in full years between datDOB and datNow.
'
' Calculates correctly for:
'   negative differences
'   leap years
'   dates of 29. February
'   date/time values with embedded time values
'   negative date/time values (prior to 1899-12-29)
'
' Optionally returns negative counts rounded down to provide a
' linear sequence of year counts.
' For a given datDOB, if datNow is decreased step wise one year from
' returning a positive count to returning a negative count, one or two
' occurrences of count zero will be returned.
' If booLinear is False, the sequence will be:
'   3, 2, 1, 0,  0, -1, -2
' If booLinear is True, the sequence will be:
'   3, 2, 1, 0, -1, -2, -3
'
' If booLinear is False, reversing datDOB and datNow will return
' results of same absolute value, only the sign will change.
' This behaviour mimics that of Fix().
' If booLinear is True, reversing datDOB and datNow will return
' results where the negative count is offset by -1.
' This behaviour mimics that of Int().

' 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.
'
' 2000-11-03. Cactus Data ApS, CPH.
' 2000-12-16. Leap year correction modified to be symmetrical.
'             Calculation of intDaysDiff simplified.
'             Renamed from YearsDiff() to Years().
' 2000-12-18. Introduced cbytMonthDaysMax.
' 2007-06-22. Version 2. Complete rewrite.
'             Check for month end of February performed with DateAdd()
'             after idea of Markus G. Fischer.
' 2008-04-27. Version 2.1.
'             Correction for calculation of linear age from Feb. 29th.
'             to Feb. 28th of a previous non-leap year as documented
'             by Lester Hui.
'
'             Example:
'               DOB: 2000-02-29, Today: 1999-02-28, Age linear: -1.
'
'             Parameters renamed to stress that for age calculations
'             first parameter must be Date of Birth as documented
'             by Markus G. Fischer.
' 2008-04-27. Version 2.2
'             Rewrite to correct negative years count bug.

  Dim intSign   As Integer
  Dim intYears  As Integer
  
  ' Find difference in calendar years.
  intYears = DateDiff("yyyy", datDOB, datNow)
  ' For positive resp. negative intervals, check if the second date
  ' falls before, on, or after the crossing date for a full 12 months period
  ' while at the same time correcting for February 29. of leap years.
  
  If DateDiff("d", datDOB, datNow) > 0 Then
    If DateDiff("d", DateAdd("yyyy", intYears, datDOB), datNow) < 0 Then
      ' The last year is not a full year.
      ' Reduce year count by one.
      intYears = intYears - 1
    End If
  Else
    intSign = Sgn(DateDiff("d", DateAdd("yyyy", -intYears, datNow), datDOB))
    If intSign <> 0 Then
      If intSign < 0 Then
        ' The last year is not a full year.
        ' Reduce negative year count by one.
        intYears = intYears + 1
      End If
      If booLinear Then
        ' Offset negative count of years to continuous sequence if requested.
        If DateDiff("d", DateAdd("yyyy", intYears, datDOB), datNow) < 0 Then
          ' Time interval includes a partial year.
          ' Increase negative year count by one.
          intYears = intYears - 1
        End If
      End If
    End If
    
  End If
  
  ' Return count of years as count of full 12 months periods.
  Years = intYears
  
End Function

Open in new window


/gustav
0
smurfer69Author Commented:
I've requested that this question be deleted for the following reason:

none of the answers were effective
0
Gustav BrockCIOCommented:
> none of the answers were effective

?
Mine is proven as very effective.

/gustav
0
smurfer69Author Commented:
these answers are incomprehensable and entirly too complicated for the qustion at hand.

Moderator <redacted> close the question
0
smurfer69Author Commented:
I've requested that this question be closed as follows:

Accepted answer: 0 points for smurfer69's comment #37873473

for the following reason:

because
0
aikimarkCommented:
This can be done entirely within the query as well as the user defined function.

Either this:
(year([date Of procedure]) - year([dob])) + clng((month([dob])*100 +Day([dob])) > (month([date Of procedure])*100+Day([date Of procedure]))) As Age

Open in new window


Or this:
(DateDiff('yyyy',[dob],[date Of procedure])) + clng((month([dob])*100 +Day([dob])) > (month([date Of procedure])*100+Day([date Of procedure]))) As Age

Open in new window


===================
Other thoughts:
* Do you have any columns in the tables named AGE?

* Since the dob field is an expression, there may be a problem when the query engine tries to substitute it into your age-on-date-of-procedure expression.  You might need to use something like this:
(year([date Of procedure]) - year(CDate([PatientDateOfBirth]))) + clng((month(CDate([PatientDateOfBirth]))*100 +Day(CDate([PatientDateOfBirth]))) > (month([date Of procedure])*100+Day([date Of procedure]))) As Age

Open in new window


* Since DateDiff() returns a variant, its Long value might be getting converted into a string or be treated as such.  That would account for your filtering (>18) throwing a data type mismatch.  If you tried >"18" as a filter, you might not encounter the error.  However, strings used as numeric values will give you wrong results.
0
Gustav BrockCIOCommented:
For a precise calculation of age, you have to use DateAdd as demonstrated above. See the in-line comments.

DateDiff does not return a string. That wouldn't make sense.

/gustav
0
aikimarkCommented:
@gustav

>>For a precise calculation of age, you have to use DateAdd as demonstrated above.
No.  Your precision is not necessary in this context.  This problem is the correct calculation of the patient's age on the date of the medical procedure.  It is a simple step function.

>>DateDiff does not return a string
I know.  I stated that it returns a variant data type that represents a long integer.  I hypothesized that the query engine might have considered the variant as a string, causing the type mismatch when compared against the numeric literal (18).
0
Gustav BrockCIOCommented:
> Your precision is not necessary in this context.  
> This problem is the correct calculation of the patient's age on the date of
> the medical procedure

I have no idea; you may be right. Normally medication is an important task but in this case we know nothing.
However, in general, I've never understood why a non-precise method should be preferred for a precise method when both are ready at hand.

/gustav
0
aikimarkCommented:
I have several applications at one of my hospital/clinic client sites.  We only need to calculate their age in (whole integer) years on the date of a procedure.

For another application, I do some interpolation of vendor-supplied age-range data.  In that case, I measure down to the day level (resolution).
0
Gustav BrockCIOCommented:
I see. Thanks.

/gustav
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Access

From novice to tech pro — start learning today.