Question

SQL 2005 - Conversion failed when converting the varchar value '6.2' to data type int.

Asked by: JeffSturgeon2002

Please note, the SQL is handled dynamically by the server, therefore some items in my WHERE clause will look odd to you - please disregard this. I am getting the following error and need a hand.

Msg 245, Level 16, State 1, Line 113
Conversion failed when converting the varchar value '6.2' to data type int.

SET NOCOUNT ON
 
CREATE TABLE #Patients (
	PatientProfileID int,
	PatientID varchar(15),
	MRN varchar (15),
	PID numeric,
	PatientName varchar(100),
	Birthdate datetime,
	Age varchar (15),
	Sex varchar(1),
	RaceCode varchar(50),
	EthnicityCode varchar(50),
	RaceMID2 int
)
 
DECLARE @AgeDate datetime
DECLARE @DXDate datetime
 
SET @AgeDate = '12/31/' + CONVERT(varchar, YEAR('08/31/2009'))
SET @DXDate = '06/30/' + CONVERT(varchar, YEAR('08/31/2009'))
 
INSERT INTO #Patients
SELECT pp.PatientProfileID, 
	pp.PatientID,
	pp.MedicalRecordNumber AS MRN,
	pp.PID,
	RTRIM(RTRIM(ISNULL(pp.Last, '') + ' ' + ISNULL(pp.Suffix, '')) + ', ' + ISNULL(pp.First, '') + ' ' + ISNULL(pp.Middle, '')) AS PatientName,  
	pp.Birthdate,
	(DATEDIFF(day,pp.Birthdate,@AgeDate) /365.25) as Age, 
	pp.Sex,
	CASE WHEN r.Code = 'H' OR e.Code = 'H' THEN 'Hispanic/Latino' 
		WHEN r.Code = 'A' THEN 'Asian'
		WHEN r.Code = 'NH' THEN 'Native Hawaiian'
		WHEN r.Code = 'OPI' THEN 'Other Pacific Islander'
		WHEN r.Code = 'B' THEN 'Black/African American'
		WHEN r.Code = 'AI' THEN 'American Indian/Alaskan Native'
		WHEN r.Code = 'W' THEN 'White'
		WHEN r.Code = 'M' THEN 'More than one race'
		WHEN r.Code = 'U' THEN 'Unreported'
		ELSE 'Unreported' END AS RaceCode,
	CASE WHEN r.Code = 'H'  OR e.Code = 'H' THEN 'Hispanic/Latino' 
		ELSE 'All Others' END AS EthnicityCode, 
	cri.RaceMID2
 
 
FROM PatientProfile pp
	LEFT JOIN PatientVisit pv ON pp.PatientProfileID = pv.PatientProfileId
	LEFT JOIN cusCRIInterview cri on pp.patientprofileid = cri.patientprofileid
	LEFT JOIN Medlists r on pp.RaceMID = r.MedListsID
	LEFT JOIN cusCRIMedLists e on cri.RaceMID2 = e.MedListsID
	
WHERE 
       --Filter on facility
	(
	(NULL IS NOT NULL AND pv.FacilityID IN (NULL)) OR
	(NULL IS NULL)
	)
     	 AND 
	--Filter on Company
	(
	(NULL IS NOT NULL AND pv.CompanyID IN (NULL)) OR
	(NULL IS NULL)
	)
	AND
	(DATEDIFF(day,pp.Birthdate,@AgeDate) /365.25) >17.99
	AND 
	DATEPART(Year,pv.visit) = DATEPART(Year,'08/31/2009') -- limit to visits in this year
	AND
	pp.PatientProfileID in -- now we filter on diagnosis, independently
	(
		SELECT PatientProfileID
		FROM PatientProfile pp
		JOIN Orders o ON pp.PID = o.PID
		JOIN Orddx  ON o.dxgroupid = orddx.dxgroupid
		WHERE
		orddx.dxcode  LIKE ('icd-401.%') 
		AND  o.XID=1000000000000000000
		AND o.OrderDate <= @DXDate
	)
GROUP BY pp.PatientProfileID,pp.PatientId, pp.MedicalRecordNumber, pp.PID, pp.Birthdate, pp.Sex, pp.Last, pp.Suffix, pp.First, pp.Middle, r.code, e.code, cri.raceMID2
HAVING COUNT(*)>1 -- must have 2+ visits this year
 
 
SELECT PatientProfileID,
	count(*) as cnt,
	PatientID,
	MRN,
	PID,
	PatientName,  
	Birthdate,
	Age, 
	Sex,
	RaceCode,
	EthnicityCode, 
	RaceMID2
INTO #tmp
FROM #Patients 
GROUP BY PatientProfileID, PatientID, MRN, PID, PatientName, Birthdate, Sex, age, RaceCode, EthnicityCode, RaceMID2
ORDER BY	CASE
	WHEN '1'  = 1 THEN MRN
	WHEN '1'  = 2 THEN PatientName 
	WHEN '1'  = 3 THEN PatientID ELSE NULL
	END
 
DECLARE @StartDate Datetime
DECLARE @EndDate Datetime
 
SET @StartDate  = CONVERT(varchar, YEAR('08/31/2009')) + '/01/01'
SET @EndDate = CONVERT(varchar, YEAR('08/31/2009')) + '/12/31'
 
ALTER TABLE #TMP ADD ObsValue varchar (2000) null
Update #TMP
Set ObsValue = '1'
FROM OBS obs
JOIN #TMP ON obs.PID = #TMP.pid
join (select pid, max(obsdate) as obsmax from obs WHERE ((HDID = 54 AND case when isnumeric(replace(obs.OBSVALUE,' mmHg',''))=1 then replace(obs.OBSVALUE,' mmHg','') else NULL end < 140) OR (HDID = 53 and case when isnumeric(replace(obs.OBSVALUE,' mmHg',''))=1 then replace(obs.OBSVALUE,' mmHg','') else NULL end < 90)) 
and obsdate >= @StartDate AND obsdate <= @EndDate group by pid) t on obs.PID=t.PID and obs.obsdate=t.obsmax
WHERE ((HDID = 54 AND case when isnumeric(replace(obs.OBSVALUE,' mmHg',''))=1 then replace(obs.OBSVALUE,' mmHg','') else NULL end < 140) OR (HDID = 53 and case when isnumeric(replace(obs.OBSVALUE,' mmHg',''))=1 then replace(obs.OBSVALUE,' mmHg','') else NULL end < 90))
 
 
Select * FROM #TMP 
Drop Table #tmp
Drop Table #Patients

                                  
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:

Select allOpen in new window

This question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.

Subscribe now for full access to Experts Exchange and get

Instant Access to this Solution

  • Plus...
  • 30 Day FREE access, no risk, no obligation
  • Collaborate with the world's top tech experts
  • Unlimited access to our exclusive solution database
  • Never be left without tech help again

Subscribe Now

Sign Up Now!
Asked On
2009-09-26 at 10:35:04ID: 24764173
Topics

SQL Server 2005

,

SQL Query Syntax

,

MS SQL Reporting

Participating Experts
2
Points
500
Comments
6

Trusted by hundreds of thousands everyday for fast, accurate and reliable tech support.

Companies like CVS, Honeywell, Chevron, Toyoto, TriStar Pictures, the U.S. Army, and Accenture

Your Complete Technology Resource

900+ topic areas (and counting)

Related Solutions

  1. conver time
    I have 2 date time fields in my database how do I convert or cast a time to display time by 12 - houir clock only such as 18:00:00 should be on the 12 hour clock for 5:00 p.m Thanks
  2. subtracting a value from an ip address in a varch…
    Hi Experts I have a feild called ip_address that I am trying to use to set the value of an other feild based on a third criteria Like this: set ip_route = (ip_address) -1 where subnet_mask <> 255.255.255.255 The probem is this,...
Tutorials are great ways to tackle hard problems!

Featured Articles

Read through articles written by top experts!

Get full access to the help you need.

Subscribe Now

30-day free trial. Register in 60 seconds.

The Latest Technology News and Tips

Your answer is only minutes away!

New Solutions Everyday

Because of our active community of experts, on average new questions receive their first comment in under 30 minutes.

Top MS SQL Server 2005 Questions

       

Hear What our Users are Saying

Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.

Subscribe Now

30-day free trial. Register in 60 seconds.

20130221-EE-VQP-038

3 Ways to Join

30-Day Free Trial

The Experts

98% positive feedback on 31,087 answers since March 2000. angeliii is a Microsoft Most Valuable Professional for his work with MS SQL Server & Development.

He has also proven his knowledge of Visual Basic Programming, PHP Scripting and Oracle Databases.

The Experts

97% positive feedback on 10,752 answers since July 2000. lrmoore has more than 18 years experience in the networking industry.

The six-time Microsoft MVPs specialties include firewalls, virtual private networking, and network management.

Testimonials

"...an excellent source for support... Kind of like having your very own IT dept." Electriciansnet

Testimonials

"I was apprehensive at signing up at first. However... it has already made my life as an IT administrator much easier." JaCrews

Testimonials

"WOW! You guys have great, active, and knowledgeable people on here." moore50

Business Clients

Business Clients

In the Press

"If you’ve got a question... Experts Exchange can supply an answer.”

In the Press

"...an invaluable aid for both IT professionals and those who require tech support."

In the Press

"where IT professionals provide quick answers on just about any topic"

Business Account Plans