Solved

datediff data mismatch

Posted on 2012-04-04
20
667 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 120

Expert Comment

by:Rey Obrero (Capricorn1)
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 120

Expert Comment

by:Rey Obrero (Capricorn1)
ID: 37807796
post the sql of the query you are using
0
Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

 

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 120

Accepted Solution

by:
Rey Obrero (Capricorn1) 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 50

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 50

Expert Comment

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

?
Mine is proven as very effective.

/gustav
0
 

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 50

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 50

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 50

Expert Comment

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

/gustav
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
Did you know that more than 4 billion data records have been recorded as lost or stolen since 2013? It was a staggering number brought to our attention during last week’s ManageEngine webinar, where attendees received a comprehensive look at the ma…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

680 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