MDX Functions - String
This section details the string manipulation functions available in Multidimensional Expressions (MDX).
Description
MDX provides a rich set of functions for working with string data. These functions allow you to manipulate, format, and extract information from string values within your Analysis Services cubes.
Key String Functions
STR()
Converts a numeric expression to a string.
STR(numeric_expression)
SELECT STR(123.45) ON COLUMNS FROM MyCube
This would return the string "123.45".
FORMAT()
Formats a value according to a specified format string.
FORMAT(value, format_string)
SELECT FORMAT(NOW(), 'yyyy-MM-dd HH:mm:ss') ON COLUMNS FROM MyCube
This would return the current date and time formatted as "YYYY-MM-DD HH:MM:SS".
LEFT()
Returns the leftmost characters of a string.
LEFT(string_expression, length)
SELECT LEFT('Microsoft SQL Server', 10) ON COLUMNS FROM MyCube
This would return the string "Microsoft ".
RIGHT()
Returns the rightmost characters of a string.
RIGHT(string_expression, length)
SELECT RIGHT('Microsoft SQL Server', 6) ON COLUMNS FROM MyCube
This would return the string "Server".
SUBSTRING()
Extracts a substring from a string.
SUBSTRING(string_expression, start_position, length)
SELECT SUBSTRING('Microsoft SQL Server', 11, 3) ON COLUMNS FROM MyCube
This would return the string "SQL".
LEN()
Returns the number of characters in a string.
LEN(string_expression)
SELECT LEN('Analysis Services') ON COLUMNS FROM MyCube
This would return the number 17.
UPPER()
Converts a string to uppercase.
UPPER(string_expression)
LOWER()
Converts a string to lowercase.
LOWER(string_expression)
REPLACE()
Replaces all occurrences of a substring with another substring.
REPLACE(string_expression, string_to_replace, replacement_string)
TRIM()
Removes leading and trailing spaces from a string.
TRIM(string_expression)
You can also specify characters to trim from the left or right using LTRIM() and RTRIM() respectively.
CONCAT()
Concatenates two or more strings.
CONCAT(string_expression1 [, string_expression2...])
Alternatively, the + operator can be used for string concatenation.