deepo
deepo

Reputation: 127

Get table name without schema

There is a string that could be a table name with ANY schema, not necessarily dbo. How do I get the table name only from the string without knowing the schema? E.g. from 'dbo.MyTable' I would need to extract 'MyTable'.

Upvotes: 1

Views: 591

Answers (1)

Salman Arshad
Salman Arshad

Reputation: 272106

PARSENAME can be used for this:

SELECT PARSENAME ('server.database.dbo.object', 1) -- object
SELECT PARSENAME ('database..object', 1)           -- object
SELECT PARSENAME ('object', 1)                     -- object
SELECT PARSENAME ('[foo bar]', 1)                  -- foo bar

Upvotes: 2

Related Questions