This table has more than 30 columns, Column name 'CODE' is the 10th column. Which is the one I selected,. I need to split this and have two columns with values 'ACH' and 'inpatient' (there are more than 15000 rows in this table with values like 'RGH . . .. . .. etc , 'PLC .. . . etc'
You need to create the function dbo.DelimitedSplit8K in your database. Look in the link that I posted in my first reply, copy the code in Figure 21 of that article, open a query window in your database, paste it and run it. That will create the function. You need to do that only once.
Replace the "col1" in the first query I posted with [CODE].
If that does not work for you, post the table DDL and sample data - like the example with the #tmp table that I posted - which someone can copy and run.
The following code is somewhat confusing/convoluted, but it should work.
SELECT
LEFT([code], CHARINDEX('|',[code])-1),
LEFT
(
STUFF([code],1,CHARINDEX('|',[code]),''),
CHARINDEX('|',STUFF([code],1,CHARINDEX('|',[code]),''))-1
)
FROM
cv3location
select substring(Code,1,3) Facility
,substring(Code,(CHARINDEX('|',Code,1)+2),9) PatientStatus
FROM [dbo].[CV3Location]
where substring(ltrim(code),1,3) = 'RGH'
and substring(Code,(CHARINDEX('|',Code,1)+2),9) = 'Inpatient'
This will give you wrong results if the Facility code is not exactly 3 characters or if the PatientStatus is not exactly 9 characters. So just check to make sure that all possible Facility codes are 3 characters long and all possible PatientStatus are 9 characters long.