List Regex Matches – Microsoft Q&A

CREATE TABLE [dbo].[RTbl](
	[rowguid] [nvarchar](100) NULL
) ON [PRIMARY]
GO
INSERT [dbo].[RTbl] ([rowguid]) VALUES (N'AF.17.48A3.8D448.4')
INSERT [dbo].[RTbl] ([rowguid]) VALUES (N'D9.8AA8.47CC9.AFF7.C')
INSERT [dbo].[RTbl] ([rowguid]) VALUES (N'E5E.0828.43S.A33B.6F38.13')
INSERT [dbo].[RTbl] ([rowguid]) VALUES (N'06.38B3.34.80W5B.F4B')
INSERT [dbo].[RTbl] ([rowguid]) VALUES (N'4.AE3B.4C')
GO

declare @pat1 varchar(100) = '%[0-9A-Z][0-9A-Z][0-9A-Z][0-9A-Z]%'
declare @pat2 varchar(100) = '%[0-9A-Z][0-9A-Z][0-9A-Z][0-9A-Z][0-9A-Z]%'

;with a as (
select [rowguid]
, case when patindex(@pat1, [rowguid]) > 0 then substring([rowguid], patindex(@pat1, [rowguid]), 4) end as pat1
, case when patindex(@pat2, [rowguid]) > 0 then substring([rowguid], patindex(@pat2, [rowguid]), 5) end as pat2
from [dbo].[RTbl]
)
select [rowguid],
case when len(pat1) > 0 and len(pat2) > 0 then pat1 + ';' + pat2 
		when len(pat1) > 0 then pat1
		when len(pat2) > 0 then pat2
end as val
from a


Here are the results.

sql

I need the val column to look like this.

val

48A3;8D448

8AA8;47CC9;AFF7

0828;A33B;6F38

38B3;80W5B

AE3B

Read more here: Source link