Hi @kasim mohamed ,
Welcome to Microsoft Q&A!
You could create one function as below:
CREATE FUNCTION [dbo].[fn_SplitString]
(
@string NVARCHAR(MAX),
@delimiter CHAR(1)
)
RETURNS @output TABLE
(
[Id] INT,
[Start] INT,
[End] INT,
[Length] INT,
[Data] NVARCHAR(MAX)
)
BEGIN
DECLARE @count INT, @start INT, @end INT
SELECT @count = 1, @end = 0,
@start = CHARINDEX(@delimiter, @string)
WHILE @start > 0 BEGIN
SELECT @end = CHARINDEX(@delimiter, @string, @start + 1)
INSERT INTO @output ([Id], [Start], [End], [Length], [Data])
VALUES (@count, @start, @end, @end - @start - 1,
SUBSTRING(@string, @start + 1, @end - @start - 1))
SELECT @start = CHARINDEX(@delimiter, @string, @end + 1),
@count = @count + 1
END
RETURN
END
Then call this function as below:
;with cte as (
select A.ID,B.data,ROW_NUMBER() over (partition by a.id order by (select null)) rn from #TxnTable A
CROSS APPLY [dbo].[fn_SplitString](FormatString,'"') B)
,cte1 as (select * from
(select id,data,rn from cte)s
pivot
(max(data) for rn in ([1],[2],[3])) p)
update a
set a.Comment=
case when b.cvalue=c.[1] and c.[2] between b.LimitFrom and b.limitto
and cast(a.givendate as date) =FORMAT(cast(c.[3] as date) ,'yyyy-dd-MM')
then 'Matched' else 'MisMatched' end
from #TxnTable a
left join #MasterTable b on a.id=b.id
left join cte1 c on c.id=a.id
select * from #TxnTable
Output:
If above is not working, please provide more sample data and expected output.
Best regards
Melissa
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.