FORMAT_STRING Contents (MDX)

 

Applies To: SQL Server 2016

The FORMAT_STRING cell property formats the VALUE cell property, creating the value for the FORMATTED_VALUE cell property. The FORMAT_STRING cell property handles both string and numeric raw values, applying a format expression against the value to return a formatted value for the FORMATTED_VALUE cell property. The following tables detail the syntax and formatting characters used to handle string and numeric values.

A format expression for strings can have one section or two sections separated by a semicolon (;).

UsageResult
One sectionThe format applies to all string values.
Two sectionsThe first section applies to string data, whereas the second section applies to null values and zero-length strings ("").

The characters described in the following table can appear in the format string for character strings.

CharacterDescription
@Represents a character placeholder that displays a character or a space. If the string has a character in the position where the at sign (@) appears in the format string, the formatted string displays the character. Otherwise, the formatted string displays a space in that position. Placeholders are filled from right to left unless there is an exclamation point (!) in the format string.
&Represents a character placeholder that displays a character or nothing. If the string has a character in the position where the ampersand (&) appears, the formatted string displays the character. Otherwise, the formatted string displays nothing. Placeholders are filled from right to left unless there is an exclamation point (!) in the format string.
<Forces lowercase. The formatted string displays all characters in lowercase format.
>Forces uppercase. The formatted string displays all characters in uppercase format.
!Forces left-to-right fill of placeholders. (The default is to fill placeholders from right to left.)

A user-defined format expression for numbers can have anywhere from one to four sections separated by semicolons. If the format argument contains one of the named numeric formats, only one section is allowed.

UsageResult
One sectionThe format expression applies to all values.
Two sectionsThe first section applies to positive values and zeros, the second to negative values.
Three sectionsThe first section applies to positive values, the second to negative values, and the third to zeros.
Four sectionsThe first section applies to positive values, the second to negative values, the third to zeros, and the fourth to null values.

The following example has two sections. The first section defines the format for positive values and zeros, and the second section defines the format for negative values.

"$#,##0;($#,##0)"  

If you include semicolons with nothing between them, the missing section prints using the format of the positive value. For example, the following format displays positive and negative values using the format in the first section and displays "Zero" if the value is zero:

"$#,##0;;\Z\e\r\o"  

The following table identifies the characters that can appear in the format string for numeric formats.

CharacterDescription
NoneDisplays the number without any formatting.
0Represents a digit placeholder that displays a digit or a zero (0).

If the number has a digit in the position where the zero appears in the format string, the formatted value displays the digit. Otherwise, the formatted value displays a zero in that position.

If the number has fewer digits than there are zeros (on either side of the decimal) in the format string, the formatted value displays leading or trailing zeros.

If the number has more digits to the right of the decimal separator than there are zeros to the right of the decimal separator in the format expression, the formatted value rounds the number to as many decimal places as there are zeros.

If the number has more digits to the left of the decimal separator than there are zeros to the left of the decimal separator in the format expression, the formatted value displays the additional digits without modification.
#Represents a digit placeholder that displays a digit or nothing.

If the expression has a digit in the position where the number sign (#) appears in the format string, the formatted value displays the digit. Otherwise, the formatted value displays nothing in that position.

The number sign (#) placeholder works like the zero (0) digit placeholder except that leading and trailing zeros are not displayed if the number has the same or fewer digits than there are # characters on either side of the decimal separator in the format expression.
.Represents a decimal placeholder that determines how many digits are displayed to the left and right of the decimal separator.

If the format expression contains only number sign (#) characters to the left of the period (.), numbers smaller than 1 start with a decimal separator. To display a leading zero displayed with fractional numbers, use zero (0) as the first digit placeholder to the left of the decimal separator.

The actual character used as a decimal placeholder in the formatted output depends on the number format recognized by the computer system.

Note: In some locales, a comma is used as the decimal separator.
%Represents a percentage placeholder. The expression is multiplied by 100. The percent character (%) is inserted in the position where the percentage appears in the format string.
,Represents a thousand separator that separates thousands from hundreds within a number that has four or more places to the left of the decimal separator.

Standard use of the thousand separator is specified if the format contains a thousand separator enclosed in digit placeholders (0 or #).

Two adjacent thousand separators, or a thousand separator immediately to the left of the decimal separator (whether or not a decimal is specified), means "scale the number by dividing the number by 1000, rounding as required." For example, you can use the format string "##0,," to represent 100 million as 100. Numbers smaller than 1 million are displayed as 0. Two adjacent thousand separators in any position other than immediately to the left of the decimal separator are treated as specifying the use of a thousand separator.

The actual character used as the thousand separator in the formatted output depends on the number format recognized by the computer system.

Note: In some locales, a period is used as the thousand separator.
:Represents a time separator that separates hours, minutes, and seconds when time values are formatted.

Note: In some locales, other characters may be used as the time separator.

The actual character used as the time separator in formatted output is determined by the system settings on the computer.
/Represents a date separator that separates the day, month, and year when date values are formatted.

The actual character used as the date separator in formatted output is determined by the system settings on the computer.

Note: In some locales, other characters may used as the date separator.
E- E+ e- e+Represents scientific format.

If the format expression contains at least one digit placeholder (0 or #) to the right of E-, E+, e-, or e+, the formatted value displays in scientific format and E or e is inserted between the number and the number's exponent. The number of digit placeholders to the right determines the number of digits in the exponent. Use E- or e- to include a minus sign next to negative exponents. Use E+ or e+ to include a minus sign next to negative exponents and a plus sign next to positive exponents.
- + $ ( )Displays a literal character.

To display a character other than one of those listed, put a backslash (\) before the character or enclose the character in double quotation marks (" ").
\Displays the next character in the format string.

To display a character that has special meaning as a literal character, put a backslash (\) before the character. The backslash itself is not displayed. Using a backslash is the same as enclosing the next character in double quotation marks. To display a backslash, use two backslashes (\\). Examples of characters that cannot be displayed as literal characters include the following characters:

 

The date-formatting and time-formatting characters—a, c, d, h, m, n, p, q, s, t, w, y, /, and :

The numeric-formatting characters—#, 0, %, E, e, comma, and period

The string-formatting characters—@, &, <, >, and !
"ABC"Displays the string inside the double quotation marks (" ").

To include a string in format from within code, use Chr(34) to enclose the text. (The character code for a double quotation mark is 34.)

Named Numeric Formats

The following table identifies the predefined numeric format names:

Format nameDescription
General NumberDisplays the number with no thousand separator.
CurrencyDisplays the number with a thousand separator, if appropriate. Displays two digits to the right of the decimal separator. Output is based on system locale settings.
FixedDisplays at least one digit to the left and two digits to the right of the decimal separator.
StandardDisplays the number with thousand separator, at least one digit to the left and two digits to the right of the decimal separator.
PercentDisplays the number multiplied by 100 with a percent sign (%) appended to the right. Always displays two digits to the right of the decimal separator.
ScientificUses standard scientific notation.
Yes/NoDisplays No if the number is 0; otherwise, displays Yes.
True/FalseDisplays False if the number is 0; otherwise, displays True.
On/OffDisplays Off if the number is 0; otherwise, displays On.

The following table identifies characters that can appear in the format string for date/time formats.

CharacterDescription
:Represents a time separator that separates hours, minutes, and seconds when time values are formatted.

The actual character used as the time separator in formatted output is determined by the system settings of the computer.

Note: In some locales, other characters may used as the time separator.
/Represents a date separator that separates the day, month, and year when date values are formatted.

The actual character used as the date separator in formatted output is determined by the system settings of the computer.

Note: In some locales, other characters may be used to represent the date separator
CDisplays the date as ddddd and displays the time as ttttt, in that order.

Displays only date information if there is no fractional part to the date serial number. Displays only time information if there is no integer portion.
dDisplays the day as a number without a leading zero (1–31).
ddDisplays the day as a number with a leading zero (01–31).
dddDisplays the day as an abbreviation (Sun–Sat).
ddddDisplays the day as a full name (Sunday–Saturday).
dddddDisplays the date as a complete date (including day, month, and year), formatted according to your system's short date format setting.

For Microsoft Windows, the default short date format is m/d/yy.
ddddddDisplays a date serial number as a complete date (including day, month, and year), formatted according to the long date setting recognized by the computer system.

For Windows, the default long date format is mmmm dd, yyyy.
wDisplays the day of the week as a number (1 for Sunday through 7 for Saturday).
wwDisplays the week of the year as a number (1–54).
mDisplays the month as a number without a leading zero (1–12).

If m immediately follows h or hh, the minute instead of the month is displayed.
mmDisplays the month as a number with a leading zero (01–12).

If m immediately follows h or hh, the minute instead of the month is displayed.
mmmDisplays the month as an abbreviation (Jan–Dec).
mmmmDisplays the month as a full month name (January–December).
qDisplays the quarter of the year as a number (1–4).
yDisplays the day of the year as a number (1–366).
yyDisplays the year as a two-digit number (00–99).
yyyyDisplays the year as a four-digit number (100–9999).
hDisplays the hour as a number without leading zeros (0–23).
hhDisplays the hour as a number with leading zeros (00–23).
nDisplays the minute as a number without leading zeros (0–59).
nnDisplays the minute as a number with leading zeros (00–59).
sDisplays the second as a number without leading zeros (0–59).
ssDisplays the second as a number with leading zeros (00–59).
t t t t tDisplays a time as a complete time (including hour, minute, and second), formatted using the time separator defined by the time format recognized by the computer system.

A leading zero is displayed if the leading zero option is selected, and the time is earlier than 10:00 in either the A.M. or the P.M. cycle. For example, 09:59,

For Windows, the default time format is h:mm:ss.
AM/PMDisplays an uppercase AM with any hour from midnight until noon; displays an uppercase PM with any hour from noon until midnight.

Note: Uses the 12-hour clock.
am/pmDisplays a lowercase am with any hour from midnight until noon; displays a lowercase pm with any hour from noon until midnight.

Note: Uses the 12-hour clock.
A/PDisplays an uppercase A with any hour from midnight until noon; displays an uppercase P with any hour from noon until midnight.

Note: Uses the 12-hour clock.
a/pDisplays a lowercase a with any hour from midnight until noon; displays a lowercase p with any hour from noon until midnight.

Note: Uses the 12-hour clock.
AMPMDisplays the AM string literal as defined by the computer system with any hour from midnight until noon; displays the PM string literal as defined by the computer system with any hour from noon until midnight.

 AMPM can be either uppercase or lowercase, but the case of the string displayed matches the string as defined by the system settings of the computer.

For Windows, the default format is AM/PM.

Note: Uses the 12-hour clock.

Named Date Formats

The following table identifies the predefined date and time format names:

Format NameDescription
General DateDisplays a date and/or time. For real numbers, displays a date and time, for example, 4/3/93 05:34 PM. If there is no fractional part, displays only a date, for example, 4/3/93. If there is no integer part, displays a time only, for example, 05:34 PM. The format of the date display is determined by your system settings.
Long DateDisplays a date according to your system's long date format.
Medium DateDisplays a date using the medium date format appropriate for the language version of the host application.
Short DateDisplays a date using your system's short date format.
Long TimeDisplays a time using your system's long time format; includes hours, minutes, and seconds.
Medium TimeDisplays a time in the 12-hour format using hours and minutes and the AM/PM designator.
Short TimeDisplays a time using the 24-hour format, for example, 17:45.

LANGUAGE and FORMAT_STRING on FORMATED_VALUE
Using Cell Properties (MDX)
Creating and Using Property Values (MDX)
MDX Query Fundamentals (Analysis Services)

Community Additions

ADD
Show: