SQL script for TestData table and functions at end of post.I have actually solved this problem but it seems to run kind of slow (over a large set of data) and I wonder if there might be a better solution.I have a column that has multiple dosages delimited by '- ' (dash + space). I need to identify if any of the dosages are duplicates within the column and then change the value of the event column depending on the result.I found a split function hanging out in the database I am playing in. So I wrote a function that uses the split function and returns the count of the duplicate dosages. I then used it like this:select Dosage_Combined , event , case when dbo.ISC_duplicate_dosage_count(Dosage_Combined) > 1 and event = 'Reorder' then 'Change' else event end as event_modified from TestData
So is there a better way of doing this?Thanks, Laurie SQL Script:Test table and data:create table TestData( event varchar (10), Dosage_Combined varchar(200))Insert into TestDataSELECT 'Active', '- 2 mg, TAB, PO (1)ea QD - 2 mg, TAB, PO (1)ea QAM - 2 mg, TAB, PO (1)ea QPM - 2 mg, TAB, PO (1)ea QPM' UNION ALLSELECT 'Reorder', '- 100 mg, TAB, PO (2)ea QAM - 100 mg, TAB, PO (1)ea QNoon - 100 mg, TAB, PO (1)ea QPM - 100 mg, TAB, PO (1)ea QPM' UNION ALLSELECT 'Active', '- 0.1 mg, TAB, PO (0.5)ea QHS - 0.1 mg, TAB, PO (0.5)ea PRN-QD' UNION ALLSELECT 'Reorder', '- 0.1 mg, TAB, PO (0.5)ea QHS - 0.1 mg, TAB, PO (0.5)ea PRN-QD' UNION ALLSELECT 'Reorder', '- 125 mg, DRC, PO (2)ea QAM - 125 mg, DRC, PO (1)ea QNoon - 125 mg, DRC, PO (2)ea QHS' UNION ALLSELECT 'Active', '- 500 mg, ERT, PO (2)ea QPM' UNION ALLSELECT 'Active', '- 25 mg, TAB, PO (1)ea QAM - 25 mg, TAB, PO (1)ea QPM - 25 mg, TAB, PO (1)ea PRN-QHS' UNION ALLSELECT 'Change', '- 5 mg, TAB, PO (1)ea As Dir. - 5 mg, TAB, PO (1)ea As Dir.' UNION ALLSELECT 'Discontinu', '- 5 mg, TAB, PO (1)ea As Dir. - 5 mg, TAB, PO (1)ea As Dir.'My Function:Create function [dbo].[ISC_duplicate_dosage_count] ( @Dosage_Combined VARCHAR(500) ) returns INTas begin DECLARE @Dosages table(Dosage varchar(255)) declare @Count int insert into @Dosages select * from fnSplit(@Dosage_Combined,'- ') set @Count = (select TOP 1 COUNT(*) FROM @Dosages GROUP BY Dosage ORDER BY COUNT(*) desc)return @CountendSplit function:create FUNCTION [dbo].[fnSplit]( @sInputList VARCHAR(8000) -- List of delimited items ,@sDelimiter VARCHAR(2)-- delimiter that separates items) RETURNS @List TABLE (item nVARCHAR(255))BEGINDECLARE @sItem VARCHAR(8000)WHILE CHARINDEX(@sDelimiter,@sInputList,0) <> 0BEGINSELECT@sItem=RTRIM(LTRIM(SUBSTRING(@sInputList,1,CHARINDEX(@sDelimiter,@sInputList,0)-1))),@sInputList=RTRIM(LTRIM(SUBSTRING(@sInputList, CHARINDEX(@sDelimiter,@sInputList,0)+LEN(@sDelimiter), LEN(@sInputList))))IF LEN(@sItem) > 0INSERT INTO @List SELECT @sItemENDIF LEN(@sInputList) > 0INSERT INTO @List SELECT @sInputList -- Put the last item inRETURNEND