Sometimes I want to start with clean information. I can always add back in the owner and identifiers.
DECLARE @vcTableName varchar(128) SET @vcTableName = 'dbo.test table' SET @vcTableName = 'mytable.test' SET @vcTableName = QUOTENAME('mytable.test table') IF CHARINDEX('[', @vcTableName) > 0 BEGIN SET @vcTableName = REPLACE(REPLACE(@vcTableName, '[', ''), ']', '') END IF CHARINDEX('.', @vcTableName) > 0 BEGIN SET @vcTableName = SUBSTRING(@vcTableName, CHARINDEX('.', @vcTableName) + 1, LEN(@vcTableName) - CHARINDEX('.', @vcTableName)) END PRINT '***' + @vcTableName + '***' PRINT 'dbo.' + QUOTENAME(@vcTableName)