Nandan Hegde

Nandan Hegde

  • 331
  • 5.5k
  • 101k

Regex expression to extract string from text via tsql

Jan 4 2024 6:39 AM

I have the below SQL text :

Select

c1,
c2,
c3

from

database.confidential.table1 t1
left join [database].dbo.table2 t2 on t1.c=t2.c
left join [database].[confidential].table1 t3 on t3.x=t2.x
inner join database.restricted.[table3] t4 on t4.y=t3.y

I want to extract below values as is from the above text :

database.confidential.table1
[database].dbo.table2
[database].[confidential].table1
database.restricted.[table3]

I tried using PATIndex with expression as '%.%.%' and substring to achieve this but was unable to achieve the result as PATIndex only gives the 1st instance of the value. So any idea/suggestion on how to proceed with the tsql?


Answers (6)