Export (0) Print
Expand All

Parse (Database Engine)

Topic Status: Some information in this topic is preview and subject to change in future releases. Preview information describes new features or changes to existing features in Microsoft SQL Server 2016 Community Technology Preview 2 (CTP2).

Parse converts the canonical string representation of a hierarchyid to a hierarchyid value. Parse is called implicitly when a conversion from a string type to hierarchyid occurs. Acts as the opposite of ToString. Parse() is a static method.

Applies to: SQL Server (SQL Server 2012 through current version), Azure SQL Database.

-- Transact-SQL syntax
hierarchyid::Parse ( input )
-- This is functionally equivalent to the following syntax 
-- which implicitly calls Parse():
CAST ( input AS hierarchyid )
-- CLR syntax
static SqlHierarchyId Parse ( SqlString input ) 


Transact-SQL: The character data type value that is being converted.

CLR: The String value that is being evaluated.

SQL Server return type: hierarchyid

CLR return type: SqlHierarchyId

If Parse receives a value that is not a valid string representation of a hierarchyid, an exception is raised. For example, if char data types contain trailing spaces, an exception is raised.

A. Converting Transact-SQL values without a table

The following code example uses ToString to convert a hierarchyid value to a string, and Parse to convert a string value to a hierarchyid.

DECLARE @StringValue AS nvarchar(4000), @hierarchyidValue AS hierarchyid
SET @StringValue = '/1/1/3/'
SET @hierarchyidValue = 0x5ADE

SELECT hierarchyid::Parse(@StringValue) AS hierarchyidRepresentation,
 @hierarchyidValue.ToString() AS StringRepresentation ;

Here is the result set.

hierarchyidRepresentation StringRepresentation

------------------------- -----------------------

0x5ADE /1/1/3/

B. CLR example

The following code snippet calls the Parse() method:

string input = “/1/2/”;
Was this page helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

© 2015 Microsoft