Reader Level:
ARTICLE

Parsing Name of Database Object Using System Function in SQL Server

On May 05, 2012 Articles | SQL Server
Returns the specified part of an object name. The parts of an object that can be retrieved are the object name, owner name, database name, and server name.
  • 0
  • 0
  • 3124

<![endif][if gte mso 9]> <![endif]-->

Returns the specified part of an object name. The parts of an object that can be retrieved are the object name, owner name, database name, and server name.

Note:  It won't check the object name in the database; it will just split by the dot and get the parsed name based on the index as listed below.

Syntax

PARSENAME ( 'object_name' , object_piece )

object_name - Name of the Database object like Table,Proc,view etc.

object_piece
                
Pass one of the following integers to parse or separate the particular piece of object.

1 = Object name

2 = Schema name

3 = Database name

4 = Server name

SQL Snippet:

USE AdventureWorks2012;

SELECT PARSENAME('AdventureWorks2012..Person', 1) AS 'Object Name';
SELECT
PARSENAME('AdventureWorks2012..Person', 2) AS 'Schema Name';
SELECT
PARSENAME('AdventureWorks2012..Person', 3) AS 'Database Name';
SELECT
PARSENAME('AdventureWorks2012..Person', 4) AS 'Server Name';
GO

img1.jpg

With Server Name:

img2.jpg 

Can Use for Custom:

Split the name by Dot Operator, no check with DB. It means if you need to parse the name based on the split by dot operator you can simply use it.

img3.jpg

  • 105rank
  • 679K readers
  • 1times
COMMENT USING

Trending up