Hi @ahmed salah ,
Try code as next:
--create function
CREATE FUNCTION dbo.GetSplitString
(
@List VARCHAR(MAX),
@Delimiter VARCHAR(255),
@ElementNumber int
)
RETURNS VARCHAR(4000)
AS
BEGIN
DECLARE @result varchar(4000)
DECLARE @Items TABLE ( position int IDENTITY PRIMARY KEY,
Item VARCHAR(4000)
)
DECLARE @ll INT = LEN(@List) + 1, @ld INT = LEN(@Delimiter);
WITH a AS
(
SELECT
[start] = 1,
[end] = COALESCE(NULLIF(CHARINDEX(@Delimiter,
@List, @ld), 0), @ll),
[value] = SUBSTRING(@List, 1,
COALESCE(NULLIF(CHARINDEX(@Delimiter,
@List, @ld), 0), @ll) - 1)
UNION ALL
SELECT
[start] = CONVERT(INT, [end]) + @ld,
[end] = COALESCE(NULLIF(CHARINDEX(@Delimiter,
@List, [end] + @ld), 0), @ll),
[value] = SUBSTRING(@List, [end] + @ld,
COALESCE(NULLIF(CHARINDEX(@Delimiter,
@List, [end] + @ld), 0), @ll)-[end]-@ld)
FROM a
WHERE [end] < @ll
)
INSERT @Items SELECT [value]
FROM a
WHERE LEN([value]) > 0
OPTION (MAXRECURSION 0);
SELECT @result=Item
FROM @Items
WHERE position=@ElementNumber
RETURN @result;
END
GO
--create table
use test
go
drop table Ref
Create table Ref
(
SignatureKey nvarchar(50),
GroupId int,
PortionKey nvarchar(50),
Status nvarchar(100)
)
insert into Ref(SignatureKey,GroupId,PortionKey,status)
values
('*$*$C$***$**$**$*',3,'s',NUll), --1
('*$*$*$FG$*$**$*',4,'F',NUll), ---2
('*$*$*$***$***$**$*',2,'g',NUll), --3
('*$**$*$***$*$**$*',5,'f',NUll) --4
--test table
select * from Ref;
--test function
SELECT dbo.GetSplitString('*$*$C$***$**$**$*','$',3);
--update table
UPDATE Ref
SET Status = CASE
WHEN dbo.GetSplitString(SignatureKey,'$',GroupId) = PortionKey THEN 'Match'
WHEN dbo.GetSplitString(SignatureKey,'$',GroupId) not like '%[^A-Z]%' then 'Not Match Charachters'
ELSE 'NULL'
END
--test after update
select * from Ref;
BR,
Mia
If the answer is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.