PKirby
PKirby

Reputation: 879

Extract string after specific data occurrence

I have the following string which varies in length depending on database and table name:

[servername].[databasename].[dbo].[tablename.csv]

I would like to extract only the table name after "[dbo]." and before ".csv]".

Any help would be greatly appreciated.

Upvotes: 0

Views: 50

Answers (1)

Mukund
Mukund

Reputation: 1689

declare @str varchar(200) = '[servername].[tablename].[dbo].[tablename.csv]'

select substring(@str,(PATINDEX('%dbo%',@str)+6), 
(PATINDEX('%.csv%', @str) - (PATINDEX('%dbo%',@str)+6)))

Upvotes: 1

Related Questions