Skip to content
Advertisement

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’.

Advertisement

Answer

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
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement