Solved

datediff data mismatch

Posted on 2012-04-04
20
627 Views
Last Modified: 2012-04-23
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
0
Comment
Question by:smurfer69
  • 5
  • 5
  • 3
  • +1
20 Comments
 
LVL 119

Expert Comment

by:Rey Obrero
ID: 37807737
check for Null values of the fields involve in your dateDiff function
0
 

Author Comment

by:smurfer69
ID: 37807786
I did, I filtered out any null values for both entries.
0
 
LVL 119

Expert Comment

by:Rey Obrero
ID: 37807796
post the sql of the query you are using
0
 

Author Comment

by:smurfer69
ID: 37807812
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
 
LVL 119

Accepted Solution

by:
Rey Obrero earned 250 total points
ID: 37807872
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
 
LVL 49

Assisted Solution

by:Gustav Brock
Gustav Brock earned 250 total points
ID: 37809775
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
 

Author Comment

by:smurfer69
ID: 37869230
I've requested that this question be deleted for the following reason:

none of the answers were effective
0
 
LVL 49

Expert Comment

by:Gustav Brock
ID: 37869231
> none of the answers were effective

?
Mine is proven as very effective.

/gustav
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

by:smurfer69
ID: 37873473
these answers are incomprehensable and entirly too complicated for the qustion at hand.

Moderator <redacted> close the question
0
 

Author Comment

by:smurfer69
ID: 37874048
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
 
LVL 45

Expert Comment

by:aikimark
ID: 37874214
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
 
LVL 49

Expert Comment

by:Gustav Brock
ID: 37874915
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
 
LVL 45

Expert Comment

by:aikimark
ID: 37875195
@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
 
LVL 49

Expert Comment

by:Gustav Brock
ID: 37875224
> 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
 
LVL 45

Expert Comment

by:aikimark
ID: 37875726
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
 
LVL 49

Expert Comment

by:Gustav Brock
ID: 37875855
I see. Thanks.

/gustav
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

In a multiple monitor setup, if you don't want to use AutoCenter to position your popup forms, you have a problem: where will they appear?  Sometimes you may have an additional problem: where the devil did they go?  If you last had a popup form open…
'Between' is such a common word we rarely think about it but in SQL it has a very specific definition we should be aware of. While most database vendors will have their own unique phrases to describe it (see references at end) the concept in common …
Familiarize people with the process of utilizing SQL Server stored procedures from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Micr…
In Microsoft Access, learn how to use Dlookup and other domain aggregate functions and one method of specifying a string value within a string. Specify the first argument, which is the expression to be returned: Specify the second argument, which …

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now