Hi @CB Weiner
You could also create a User-defined Function.
Please check this:
GO
Create or Alter function UDF_ExtractElements_1
(
@input nvarchar(100)
)
Returns varchar(100)
As
Begin
Declare @numberIndex_5 int = Patindex('%[0-9A-Z][0-9A-Z][0-9A-Z][0-9A-Z][0-9A-Z]%', @input)
Declare @numberIndex_4 int = Patindex('%[0-9A-Z][0-9A-Z][0-9A-Z][0-9A-Z]%', @input)
Declare @Output varchar(100) =''
Begin
While @numberIndex_5 > 0 or @numberIndex_4 > 0
Begin
Set @Output = @Output+
Case When @numberIndex_4 = 0 Then SUBSTRING(@input,@numberIndex_5,5)+';'
When @numberIndex_5 = 0 Then SUBSTRING(@input,@numberIndex_4,4)+';'
When @numberIndex_4 <= @numberIndex_5 Then SUBSTRING(@input,@numberIndex_4,4)+';'
When @numberIndex_4 > @numberIndex_5 Then SUBSTRING(@input,@numberIndex_5,5)+';'
End
Set @input = Case When @numberIndex_4 = 0 Then STUFF(@input,@numberIndex_5,5,'')
When @numberIndex_5 = 0 Then STUFF(@input,@numberIndex_4,4,'')
When @numberIndex_4 <= @numberIndex_5 Then STUFF(@input,@numberIndex_4,4,'')
When @numberIndex_4 > @numberIndex_5 Then STUFF(@input,@numberIndex_5,5,'')
End
Set @numberIndex_5 = Patindex('%[0-9A-Z][0-9A-Z][0-9A-Z][0-9A-Z][0-9A-Z]%', @input)
Set @numberIndex_4 = Patindex('%[0-9A-Z][0-9A-Z][0-9A-Z][0-9A-Z]%', @input)
End
End
Return @Output
End
GO
SELECT [rowguid],LEFT(dbo.UDF_ExtractElements_1([rowguid]),LEN(dbo.UDF_ExtractElements_1([rowguid]))-1) AS VAL
FROM [RTbl]
Best regards,
Cosmog Hong
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
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.