Using String Functions

THIS TOPIC APPLIES TO:yesSQL Server (starting with 2008)noAzure SQL DatabasenoAzure SQL Data Warehouse noParallel Data Warehouse

You can use string functions on nearly every object in Multidimensional Expressions (MDX). In stored procedures, you use string functions primarily to convert the object to a string representation. You also use string functions to evaluate a string expression over an object in order to return a value.

The most widely used string functions are Name and Uniquename. Respectively, these functions return the name and unique name of an object. Mostly, they are used when debugging calculations to discover what member a function is returning.


The following example queries show how to use these functions:


//Returns the name of the current Product on rows

MEMBER [Measures].[ProductName] AS [Product].[Product].CurrentMember.Name

//Returns the uniquename of the current Product on rows

MEMBER [Measures].[ProductUniqueName] AS [Product].[Product].CurrentMember.Uniquename

//Returns the name of the Product dimension

MEMBER [Measures].[ProductDimensionName] AS [Product].Name

SELECT {[Measures].[ProductName],[Measures].[ProductUniqueName],[Measures].[ProductDimensionName]}


[Product].[Product].MEMBERS ON ROWS

FROM [Adventure Works]

The Generate function can be used to execute a string function on every member of a set and concatenate the results. This also can be useful when debugging calculations as it allows you to visualize the contents of a set. The following example shows how to use it in this way:


//Returns the names of the current Product and its ancestors up to the All Member

MEMBER [Measures].[AncestorNames] AS


ASCENDANTS([Product].[Product Categories].CurrentMember)

, [Product].[Product Categories].CurrentMember.Name, ", ")




[Product].[Product Categories].MEMBERS ON ROWS

FROM [Adventure Works]

Another group of widely used string functions are those that enable you to cast a string containing the uniquename of an object or an expression which resolves to the object into the object itself. The following example query demonstrates how the StrToMember and StrToSet functions do this:


{StrToMember("[Measures].[Inter" + "net Sales Amount]")}



[Product].[Product Categories].[Category].&[3],

[Product].[Product Categories].[Product].&[477],

[Product].[Product Categories].[Product].&[788],

[Product].[Product Categories].[Product].&[708],

[Product].[Product Categories].[Product].&[711]



FROM [Adventure Works]


The StrToMember and StrToSet functions should be used with caution. They can lead to poor query performance if they are used within calculation definitions.

See Also

Generate (MDX)
Name (MDX)
UniqueName (MDX)
Functions (MDX Syntax)
Using Stored Procedures (MDX)
StrToMember (MDX)
StrToSet (MDX)