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?

Nandan HegdePosted Jan 4, 2024, 10:36 AM
Thank you Naveen for your reply.
Here the need is to maintain the values as is ,
the expected output is as below :
database.confidential.table1
[database].dbo.table2
[database].[confidential].table1
database.restricted.[table3]
and with the query you provided, it would never have the brackets. We want the values as is
Naveen KumarPosted Jan 4, 2024, 8:25 AM
You can use CHARINDEX and PARSENAME to achive it but the cache here is, somewhere like database or schema or table name you might have [ ] brackets. So excluding the brackets we can get it.
Try the below query:
Output returned
Naimish MakwanaPosted Jan 4, 2024, 7:36 AM
To handle the generic case where you want to extract patterns like
[abc].xyz.lmnor12345.[123].[890], you can use a regular expression-based approach. Unfortunately, SQL Server does not have built-in regular expression functions, but you can use a combination of PATINDEX, SUBSTRING, and CHARINDEX to achieve a similar result.Here is an example T-SQL script:
Thanks
Nandan HegdePosted Jan 4, 2024, 6:57 AM
Thank you Naimish for your input but hardcoding on from and Join keyword may not work as the above was just a sample example.
The use case is based on generic for below example as well like
text is :
This is a poc for [abc].xyz.lmn or 12345.[123].[890]
So for the sbove text I need the below output:
[abc].xyz.lmn
12345.[123].[890]
also thank you Jayraj but in your case, not getting the exact output for the 3rd row :
database.restricted.[table3] since the regex is on []
Jayraj ChhayaPosted Jan 4, 2024, 6:57 AM
Hi Nandan Hegde,
To extract specific values from the given SQL text using T-SQL, you can use a combination of string manipulation functions such as
CHARINDEX,SUBSTRING, andPATINDEX. Here's an example of how you can achieve this:The
PATINDEXfunction is used to find the starting index of the desired strings, and theCHARINDEXfunction is used to find the ending index. We then use theSUBSTRINGfunction to extract the value between the starting and ending indices.The extracted values are stored in a table variable
@extractedValues, and you can retrieve them by selecting from this table.Please note that this example assumes that the desired values are enclosed in square brackets (
[]). If the format of the values is different, you may need to modify the pattern used in thePATINDEXfunction accordingly.Naimish MakwanaPosted Jan 4, 2024, 6:41 AM
Try below
Thanks