Peryan77:
I suppose I misunderstood your intent. I thought you wanted to delete the whole row if it started with 00,01, or 02.
Instead try this:
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO
create function dbo.ufsDelmListToTable
(
@vchList varchar(8000),
@chDelimit char(1)
)
/************************************************** **********
* Goal: Convert a delimited list into a table (each item as its own row)
* Created by David Lundell, Principal Consultant and Trainer
* Mutually Beneficial Inc Copyright 2006
* Provided as is with no warranties express or implied
************************************************** ***********
*/
RETURNS @tblList TABLE (vchChar varchar(255)) AS
begin
declare @iStart int,
@iEnd int
--Extract the list
select @iStart = 1
Select @iEnd = charindex(@chDelimit,@vchList)
while @iEnd > 0
begin
insert @tblList (vchChar)
select LTRIM(RTRIM(substring(@vchList, @iStart, @iEnd - @iStart)))
select @iStart = @iEnd + 1
Select @iEnd = charindex(@chDelimit,@vchList, @iStart)
end
--Get last value
insert @tblList (vchChar)
select LTRIM(RTRIM(substring(@vchList, @iStart, len(@vchList))))
return
end
/* Test Scenario
Select * from dbo.ufsDelmListToTable('1,2,3,4,5,6,7,8',',')
*/
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO
create function dbo.ufs_DelElem_in_DelmList
(
@vchList varchar(8000)
,@chDelimit varchar(3)
,@DeleteMask varchar(10)
,@chNewDelimit varchar(3) = NULL
)
/************************************************** **********
* Goal: Remove certain items from a delimited list @vchList
* (@chDelimit is the delimiter)
* based on @DeleteMask as the criteria for NOT LIKE
* and optionally replace the delimiter with @chNewDelimit
* Depends on ufsDelmListToTable
* Created by David Lundell, Principal Consultant and Trainer
* Mutually Beneficial Inc Copyright 2006
* Provided as is with no warranties express or implied
************************************************** ***********
*/
RETURNS varchar(8000) AS
begin
declare @iListCount int,
@iRowNum int,
@vchstrList varchar(8000),
@vchItem varchar(255)
SELECT @chNewDelimit = ISNULL(@chNewDelimit,@chDelimit)
--Extract the list
DECLARE @tblList TABLE (tID int Identity(1,1) , vchChar varchar(255))
INSERT @tblList (vchChar)
SELECT vchChar
FROM dbo.ufsDelmListToTable(@vchList, @chDelimit)
WHERE vchChar NOT LIKE @DeleteMask
SELECT @iListCount = (Select Count(*) from @tblList)
SELECT @iRowNum = 1
WHILE @iRowNum < @iListCount
BEGIN
SELECT TOP 1 @vchstrList = ISNULL(@vchstrList,'') + vchChar + @chNewDelimit
FROM @tblList
WHERE tID <= @iRowNum
ORDER BY tID DESC
SELECT @iRowNum = @iRowNum + 1
END
IF (@iRowNum = @iListCount)
BEGIN
SELECT TOP 1 @vchstrList = ISNULL(@vchstrList,'') + vchChar
FROM @tblList
WHERE tID <= @iRowNum
ORDER BY tID DESC
END
return (@vchstrList)
end
/* Test Scenario
CREATE TABLE #ActTable
(actID int Identity(1,1)
,AllowedAccounts varchar(8000)
)
INSERT #ActTable (AllowedAccounts) VALUES ('00_Report234, 01_Report393, 03_Report59, 04_Report81, 05_Reports84') -- first two
INSERT #ActTable (AllowedAccounts) VALUES ('00_Report234, 02_Report393, 03_Report59') -- first two
INSERT #ActTable (AllowedAccounts) VALUES ('03_Report234, 04_Report393, 05_Report59') -- won't be affected
INSERT #ActTable (AllowedAccounts) VALUES ('00_Report234, 01_Report393, 02_Report59') -- all accounts affected
INSERT #ActTable (AllowedAccounts) VALUES ('00_Report234, 05_Report393, 04_Report81, 05_Reports84, 01_Report59') -- first and last
SELECT actID, AllowedAccounts, dbo.ufs_DelElem_in_DelmList( AllowedAccounts, ',','0[0-2]%',', ')
FROM #ActTable
DROP TABLE #ActTable
*/
David Lundell
Principal Consultant and Trainer
www.mutuallybeneficial.com