Hi All,
I have a table with 3 columns ID(int), PhysicianName, and Text(varchar(max)). I am trying to parse a text column with multiple physicians but need to extract only one physician info based on PhysicianName column.
Here is the sample source data.
ID PhysicianName Text
1 Roddy Patty This is a combined notes of the Physician appointments on a given day. Roddy Patty Profession: General Physician ; PhysicianID: 123ABC ; Appointment Date: January 7, 2001 VisitDescription: Patient joined due to bla bla blah. Summary: Patient had bla bla blah. Michael Duke Profession: Surgeon ; PhysicianID: 567XYZ ; Appointment Date: March 10, 2011 visitDescription: Patient bla bla blah. Summary: Patient suffering from bla bla blah need immediate surgeory & treatment.
2 John Smith This is a combined notes of the Physician appointments on a given day. Jake Dawn Profession: Physician ; PhysicianID: 453AAA Appointment Date: October 09, 2009 VisitDescription: Patient joined due to bla bla blah. Summary: Patient had bla bla blah. John Smith Profession: Civil Surgeon ; PhysicianID: 123BBB ; Appointment Date: June 15, 2005 VisitDescription: Patient had bla bla blah. Summary: Patient suffering from bla bla blah need to start treatment.
Text column contains series of text with all physicians info(on a given day text column can have 1 or more physicians info). I want a query which should substring single physician info per ID based on PhysicianName column.
For example: ID = 1 , It has two Physicians in the text - Roddy Patty & Michael Duke, but I want only Roddy Patty Physician information because for ID=1 the Physician is Roddy Patty.
For PhysicianName = "Roddy Patty", the query should substring all information from text column from "Roddy Patty Profession: General Physician ; PhysicianID: 123ABC ; Appointment Date: January 7, 2001 VisitDescription: Patient joined due to bla bla blah. Summary: Patient had bla bla blah" and exclude Michael Duke info.
Need output for ID =1:
ID PhysicianName Profession PhysicianID AppointmentDate VisitDescription Summary
1 Roddy Patty General Physician 123ABC January 7, 2001 Patient joined due to bla bla blah. Patient had bla bla blah
Same thing with ID = 2, It has two Physicians in the text - Jake Dawn & John Smith, but I want only John Smith Physician information because for ID=2 the Physician is John Smith, and exclude Jake Dawn info.
For PhysicianName = "John Smith", the query should substring all information from text column from "John Smith Profession: Civil Surgeon ; PhysicianID: 123BBB ; Appointment Date: June 15, 2005 VisitDescription: Patient had bla bla blah. Summary: Patient suffering from bla bla blah need to start treatment."
Need output for ID =2:
ID PhysicianName Profession PhysicianID AppointmentDate VisitDescription Summary
2 John Smith Civil Surgeon 123BBB June 15, 2005 Patient had bla bla blah. Patient suffering from bla bla blah need to start treatment.
I know that we have to use Substring , charindex or Patindex to extract the data, I have tried it but no luck, getting Invalid length parameter passed to the LEFT or SUBSTRING function error.
SELECT SUBSTRING(Text, CHARINDEX(PhysicianName, Text), CHARINDEX('Profession:',Text) - CHARINDEX(PhysicianName, Text) + 500)
It would be helpful if someone can post the code to parse this kind of data.
Create Statement:
CREATE Table #Physician (ID INT , PhysicianName Varchar(30) , Text Varchar(max))
Insert Statement:
Insert into #Physician (ID , PhysicianName , Text )
values ( 1 , 'Roddy Patty' , 'This is a combined notes of the Physician appointments on a given day. Roddy Patty Profession: General Physician ; PhysicianID: 123ABC ; Appointment Date: January 7, 2001 VisitDescription: Patient joined due to bla bla blah. Summary: Patient had bla bla blah. Michael Duke Profession: Surgeon ; PhysicianID: 567XYZ ; Appointment Date: March 10, 2011 visitDescription: Patient bla bla blah. Summary: Patient suffering from bla bla blah need immediate surgeory & treatment.' ),
(2 , 'John Smith' , 'This is a combined notes of the Physician appointments on a given day. Jake Dawn Profession: Physician ; PhysicianID: 453AAA Appointment Date: October 09, 2009 VisitDescription: Patient joined due to bla bla blah. Summary: Patient had bla bla blah. John Smith Profession: Civil Surgeon ; PhysicianID: 123BBB ; Appointment Date: June 15, 2005 VisitDescription: Patient had bla bla blah. Summary: Patient suffering from bla bla blah need to start treatment.')
drop table #Physician
Thanks in advance,
RH