TEXT
Updated 2023-11-10 16:08:55.653000
Syntax
SELECT [westclintech].[wct].[TEXT] (
<@Value, float,>
,<@Format, nvarchar(4000),>)
Description
Use the scalar function TEXT to convert a numeric value to text and specify the display formatting by using special format strings.
Arguments
@Value
is the value to be formatted. @Value is an expression of type float or of a type that can be implicitly converted to float.
@Format
A numeric format as a text string enclosed in quotation marks, for example 'm/d/yyyy' or '#,###.00'. The @Format argument can be of data types that are implicitly convertible to nvarchar or ntext.
Return Type
nvarchar(4000)
Remarks
TEXT treats date formats by interpreting the number as the number of days since December 31, 1899, consistent with EXCEL. This is different than how SQL Server stores date in a datetime field
Use zero (0) in @Format to display insignificant zeroes if @Value has fewer digits than there are in the format. For example, if you always wanted to display 5 decimal places to the right of the decimal, then @Format would end in '.00000'.
Use the symbol (#) in @Format to suppress leading and trailing zeroes. If you wanted to display up to 5 decimal places, but no more, then @Format would end in '.#####'.
Use the symbol (?) in @Format to display spaces in place of insignificant zeroes if @Value has fewer digits than there are in @Format. This could be used to make sure that the decimal places are lined up in the resultant table.
Use the period (.) in @Format to display the decimal point in a number.
Use the comma (,) in @Format as the thousands separator.
A comma (,) at the end of @Format scales @Value by one thousand. You can use multiple commas; each one scaling by one thousand, so 2 commas would scale by one million, 3 commas by one billion, etc.
Use the symbol (/) in @Format to identify that you want the result returned as a fraction. The (/) symbol can be used in conjunction with the other symbols. You cannot have more than 3 digits in the denominator. If you need fractions with more than 3 digits, use the DEC2FRAC function.
Put the dollar grammalogue ($) at the beginning of @Format to display a currency value. You can specify other currencies by using the appropriate symbols: (¢), (£), (¥), and (€).
Use the percent sign (%) in @Format to display @Value as a percent.
Use the letter E (E, e, E+, E-, e+, e-) in @Format to display @Value in scientific notation. For example, if @Format = '0.00E+00' and @Value = 299792458, then the result would be 3.00E+08. If @Format = '0.###E+00', then the result would be 2.998E+08.
Use the semi-colon (;) to identify different formats for positive and negative values or positive, negative, and zero values.
Use m to display the month as a number without a leading zero.
Use mm to display the month as a number with a leading zero when appropriate.
Use mmm to display the month as an abbreviation (Jan to Dec).
Use mmmm to display the month as a full name (January to December).
Use mmmmm to display the month as a single letter (J to D).
Use d to displays the day as a number without a leading zero.
Use dd to display the day as a number with a leading zero when appropriate.
Use ddd to display the day as an abbreviation (Sun to Sat).
Use dddd to display the day as a full name (Sunday to Saturday).
Use yy to display the year as a two-digit number.
Use yyyy to display the year as a four-digit number.
Use h to display the hour as a number without a leading zero.
Use [h] to display elapsed time in hours. If you are working with a formula that returns a time in which the number of hours exceeds 24, use a number format that resembles [h]:mm:ss.
Use hh to display the hour as a number with a leading zero when appropriate. If the format contains AM or PM, the hour is shown based on the 12-hour clock. Otherwise, the hour is shown based on the 24-hour clock.
Use m to Display the minute as a number without a leading zero. The m or the mm code must appear immediately after the h or hh code or immediately before the ss code; otherwise, the month is displayed instead of minutes.
Use [m] to display elapsed time in minutes. If you are working with a formula that returns a time in which the number of minutes exceeds 60, use a number format that resembles [mm]:ss.
Use mm to display the minute as a number with a leading zero when appropriate. The m or the mm code must appear immediately after the h or hh code or immediately before the ss code; otherwise, the month is displayed instead of minutes.
Use s to display the second as a number without a leading zero.
Use [s] to display elapsed time in seconds. If you are working with a formula that returns a time in which the number of seconds exceeds 60, use a number format that resembles [ss].
Use ss to display the second as a number with a leading zero when appropriate. If you want to display fractions of a second, use a number format that resembles h:mm:ss.00.
Use AM/PM, am/pm, A/P, or a/p to displays the hour based on a 12-hour clock.
Examples
These are some basic examples dealing with decimal places and significant digits. We have used a temporary table to simplify the examples.
CREATE TABLE #t
(
[recno] [float] NOT NULL,
[num_val] [float] NOT NULL,
[num_format] [varchar](50) NOT NULL
);
INSERT INTO #t
VALUES
(1, 1234567890, '#,#');
INSERT INTO #t
VALUES
(2, 1234567890, '###');
INSERT INTO #t
VALUES
(3, 1234567890, '###.##');
INSERT INTO #t
VALUES
(4, 1234567890, '###.00');
INSERT INTO #t
VALUES
(5, 22.98643, '###.000');
INSERT INTO #t
VALUES
(6, 22.98643, '0.#');
INSERT INTO #t
VALUES
(7, 22.98643, '#.0#');
INSERT INTO #t
VALUES
(8, 44.398, '???.???');
INSERT INTO #t
VALUES
(9, 102.65, '???.???');
INSERT INTO #t
VALUES
(10, 2.8, '???.???');
INSERT INTO #t
VALUES
(11, 5.25, '# ???/???');
INSERT INTO #t
VALUES
(12, 5.3, '# ???/???');
select *,
wct.TEXT(num_val, num_format)
from #t;
This produces the following result.
{"columns":[{"field":"recno","headerClass":"ag-right-aligned-header","cellClass":"ag-right-aligned-cell"},{"field":"num_val","headerClass":"ag-right-aligned-header","cellClass":"ag-right-aligned-cell"},{"field":"num_format"},{"field":" "}],"rows":[{"recno":"1","num_val":"1234567890","num_format":"#,#"," ":"1,234,567,890"},{"recno":"2","num_val":"1234567890","num_format":"###"," ":"1234567890"},{"recno":"3","num_val":"1234567890","num_format":"###.##"," ":"1234567890."},{"recno":"4","num_val":"1234567890","num_format":"###.00"," ":"1234567890.00"},{"recno":"5","num_val":"22.98643","num_format":"###.000"," ":"22.986"},{"recno":"6","num_val":"22.98643","num_format":"0.#"," ":"23."},{"recno":"7","num_val":"22.98643","num_format":"#.0#"," ":"22.99"},{"recno":"8","num_val":"44.398","num_format":"???.???"," ":"44.398"},{"recno":"9","num_val":"102.65","num_format":"???.???"," ":"102.65"},{"recno":"10","num_val":"2.8","num_format":"???.???"," ":"2.8"},{"recno":"11","num_val":"5.25","num_format":"# ???/???"," ":"5 1/4"},{"recno":"12","num_val":"5.3","num_format":"# ???/???"," ":"5 3/10"}]}
Scientific notation:
CREATE TABLE #t
(
[recno] [int] NOT NULL,
[num_val] [float] NOT NULL,
[num_format] [varchar](15) NOT NULL
);
INSERT INTO #t
VALUES
(13, 1234567890, '0.00E00');
INSERT INTO #t
VALUES
(14, 1234567890, '#.##E00');
INSERT INTO #t
VALUES
(15, 1234567890, '0.00E+00');
INSERT INTO #t
VALUES
(16, 1234567890, '#.##E+00');
INSERT INTO #t
VALUES
(17, 1234567890, '0.00E-00');
INSERT INTO #t
VALUES
(18, 1234567890, '#.##E-00');
INSERT INTO #t
VALUES
(19, .00001234567890, '0.000E00');
INSERT INTO #t
VALUES
(20, .00001234567890, '#.###E00');
INSERT INTO #t
VALUES
(21, .00001234567890, '0.00E+00');
INSERT INTO #t
VALUES
(22, .00001234567890, '#.##E+00');
INSERT INTO #t
VALUES
(23, .00001234567890, '0.00E-0');
INSERT INTO #t
VALUES
(24, .00001234567890, '#.##E-0');
select *,
wct.TEXT(num_val, num_format)
from #t;
This produces the following result.
{"columns":[{"field":"recno","headerClass":"ag-right-aligned-header","cellClass":"ag-right-aligned-cell"},{"field":"num_val","headerClass":"ag-right-aligned-header","cellClass":"ag-right-aligned-cell"},{"field":"num_format"},{"field":" ","headerClass":"ag-right-aligned-header","cellClass":"ag-right-aligned-cell"}],"rows":[{"recno":"13","num_val":"1234567890","num_format":"0.00E00"," ":"1.23E09"},{"recno":"14","num_val":"1234567890","num_format":"#.##E00"," ":"1.23E09"},{"recno":"15","num_val":"1234567890","num_format":"0.00E+00"," ":"1.23E+09"},{"recno":"16","num_val":"1234567890","num_format":"#.##E+00"," ":"1.23E+09"},{"recno":"17","num_val":"1234567890","num_format":"0.00E-00"," ":"1.23E09"},{"recno":"18","num_val":"1234567890","num_format":"#.##E-00"," ":"1.23E09"},{"recno":"19","num_val":"1.23456789E-05","num_format":"0.000E00"," ":"1.235E-05"},{"recno":"20","num_val":"1.23456789E-05","num_format":"#.###E00"," ":"1.235E-05"},{"recno":"21","num_val":"1.23456789E-05","num_format":"0.00E+00"," ":"1.23E-05"},{"recno":"22","num_val":"1.23456789E-05","num_format":"#.##E+00"," ":"1.23E-05"},{"recno":"23","num_val":"1.23456789E-05","num_format":"0.00E-0"," ":"1.23E-5"},{"recno":"24","num_val":"1.23456789E-05","num_format":"#.##E-0"," ":"1.23E-5"}]}
Thousands separator:
CREATE TABLE #t
(
[recno] [int] NOT NULL,
[num_val] [float] NOT NULL,
[num_format] [varchar](15) NOT NULL
);
INSERT INTO #t
VALUES
(25, 1234567890, '#,###');
INSERT INTO #t
VALUES
(26, 1234567890, '#,###,');
INSERT INTO #t
VALUES
(27, 1234567890, '#,###,,');
select *,
wct.TEXT(num_val, num_format)
from #t;
This produces the following result.
{"columns":[{"field":"recno","headerClass":"ag-right-aligned-header","cellClass":"ag-right-aligned-cell"},{"field":"num_val","headerClass":"ag-right-aligned-header","cellClass":"ag-right-aligned-cell"},{"field":"num_format"},{"field":" "}],"rows":[{"recno":"25","num_val":"1234567890","num_format":"#,###"," ":"1,234,567,890"},{"recno":"26","num_val":"1234567890","num_format":"#,###,"," ":"1,234,568"},{"recno":"27","num_val":"1234567890","num_format":"#,###,,"," ":"1,235"}]}
Dates:
CREATE TABLE #t
(
[recno] [int] NOT NULL,
[num_val] [float] NOT NULL,
[num_format] [varchar](20) NOT NULL
);
INSERT INTO #t
VALUES
(28, 39933, 'mm/dd/yy');
INSERT INTO #t
VALUES
(29, 39933, 'dd/mm/yy');
INSERT INTO #t
VALUES
(30, 39933, 'd/m/yy');
INSERT INTO #t
VALUES
(31, 39933, 'm/d/yy');
INSERT INTO #t
VALUES
(32, 39933, 'mm/dd/yyyy');
INSERT INTO #t
VALUES
(33, 39933, 'dd/mm/yyyy');
INSERT INTO #t
VALUES
(34, 39933, 'd/m/yyyy');
INSERT INTO #t
VALUES
(35, 39933, 'm/d/yyyy');
INSERT INTO #t
VALUES
(36, 39933, 'yymmdd');
INSERT INTO #t
VALUES
(37, 39933, 'dddd, dd mmmm yyyy');
INSERT INTO #t
VALUES
(38, 39933, 'ddd, dd mmm yyyy');
INSERT INTO #t
VALUES
(39, 39933, 'mmmmm-yyyy');
select *,
wct.TEXT(num_val, num_format)
from #t;
This produces the following result.
{"columns":[{"field":"recno","headerClass":"ag-right-aligned-header","cellClass":"ag-right-aligned-cell"},{"field":"num_val","headerClass":"ag-right-aligned-header","cellClass":"ag-right-aligned-cell"},{"field":"num_format"},{"field":" "}],"rows":[{"recno":"28","num_val":"39933","num_format":"mm/dd/yy"," ":"04/30/09"},{"recno":"29","num_val":"39933","num_format":"dd/mm/yy"," ":"30/04/09"},{"recno":"30","num_val":"39933","num_format":"d/m/yy"," ":"30/4/09"},{"recno":"31","num_val":"39933","num_format":"m/d/yy"," ":"4/30/09"},{"recno":"32","num_val":"39933","num_format":"mm/dd/yyyy"," ":"04/30/2009"},{"recno":"33","num_val":"39933","num_format":"dd/mm/yyyy"," ":"30/04/2009"},{"recno":"34","num_val":"39933","num_format":"d/m/yyyy"," ":"30/4/2009"},{"recno":"35","num_val":"39933","num_format":"m/d/yyyy"," ":"4/30/2009"},{"recno":"36","num_val":"39933","num_format":"yymmdd"," ":"090430"},{"recno":"37","num_val":"39933","num_format":"dddd, dd mmmm yyyy"," ":"Thursday, 30 April 2009"},{"recno":"38","num_val":"39933","num_format":"ddd, dd mmm yyyy"," ":"Thu, 30 Apr 2009"},{"recno":"39","num_val":"39933","num_format":"mmmmm-yyyy"," ":"A-2009"}]}
Times:
CREATE TABLE #t
(
[recno] [int] NOT NULL,
[num_val] [float] NOT NULL,
[num_format] [varchar](20) NOT NULL
);
INSERT INTO #t
VALUES
(40, 0.5892361, 'h:m:s');
INSERT INTO #t
VALUES
(41, 0.5892361, 'hh:mm:ss');
INSERT INTO #t
VALUES
(42, 0.5892361, 'h:mm:ss AM/PM');
INSERT INTO #t
VALUES
(43, 0.5892361, 'h:mm AM/PM');
INSERT INTO #t
VALUES
(44, 0.5892361, '[hh]:mm:ss');
INSERT INTO #t
VALUES
(45, 0.5892361, '[mm]:ss');
INSERT INTO #t
VALUES
(46, 0.5892361, '[ss]');
select *,
wct.TEXT(num_val, num_format)
from #t;
This produces the following result.
{"columns":[{"field":"recno","headerClass":"ag-right-aligned-header","cellClass":"ag-right-aligned-cell"},{"field":"num_val","headerClass":"ag-right-aligned-header","cellClass":"ag-right-aligned-cell"},{"field":"num_format"},{"field":" "}],"rows":[{"recno":"40","num_val":"0.5892361","num_format":"h:m:s"," ":"14:8:30"},{"recno":"41","num_val":"0.5892361","num_format":"hh:mm:ss"," ":"14:08:30"},{"recno":"42","num_val":"0.5892361","num_format":"h:mm:ss AM/PM"," ":"2:08:30 PM"},{"recno":"43","num_val":"0.5892361","num_format":"h:mm AM/PM"," ":"2:08 PM"},{"recno":"44","num_val":"0.5892361","num_format":"[hh]:mm:ss"," ":"14:08:30"},{"recno":"45","num_val":"0.5892361","num_format":"[mm]:ss"," ":"848:30"},{"recno":"46","num_val":"0.5892361","num_format":"[ss]"," ":"50910"}]}
Date & Time:
CREATE TABLE #t
(
[recno] [int] NOT NULL,
[num_val] [float] NOT NULL,
[num_format] [varchar](25) NOT NULL
);
INSERT INTO #t
VALUES
(47, 39933.5892361, 'm/d/yy h:m:s');
INSERT INTO #t
VALUES
(48, 39933.5892361, 'mm/dd/yyyy hh:mm:ss');
INSERT INTO #t
VALUES
(49, 39933.5892361, 'mm/dd/yyyy h:mm AM/PM');
INSERT INTO #t
VALUES
(50, 39933.5892361, 'dddd, mm/dd/yy h:mm AM/PM');
select *,
wct.TEXT(num_val, num_format)
from #t;
This produces the following result.
{"columns":[{"field":"recno","headerClass":"ag-right-aligned-header","cellClass":"ag-right-aligned-cell"},{"field":"num_val","headerClass":"ag-right-aligned-header","cellClass":"ag-right-aligned-cell"},{"field":"num_format"},{"field":" ","headerClass":"ag-right-aligned-header","cellClass":"ag-right-aligned-cell"}],"rows":[{"recno":"47","num_val":"39933.5892361","num_format":"m/d/yy h:m:s"," ":"04/30/09 14:8:30"},{"recno":"48","num_val":"39933.5892361","num_format":"mm/dd/yyyy hh:mm:ss"," ":"04/30/2009 14:08:30"},{"recno":"49","num_val":"39933.5892361","num_format":"mm/dd/yyyy h:mm AM/PM"," ":"04/30/2009 2:08 PM"},{"recno":"50","num_val":"39933.5892361","num_format":"dddd, mm/dd/yy h:mm AM/PM"," ":"Thursday, 04/30/09 2:08 PM"}]}
Percent, currencies, and semi-colons:
CREATE TABLE #t
(
[recno] [int] NOT NULL,
[num_val] [float] NOT NULL,
[num_format] [varchar](25) NOT NULL
);
INSERT INTO #t
VALUES
(51, 1.5, '#.00%');
INSERT INTO #t
VALUES
(52, -0.75, '#.00%');
INSERT INTO #t
VALUES
(53, 1000000, '$#,#.00');
INSERT INTO #t
VALUES
(54, -1000000, '$#,#.00');
INSERT INTO #t
VALUES
(55, 1000000, '€#,#.00');
INSERT INTO #t
VALUES
(56, -1000000, '€#,#.00;(€#,#.00)');
INSERT INTO #t
VALUES
(57, 1000000, '¥#,#;(¥#,#)');
INSERT INTO #t
VALUES
(58, -1000000, '¥#,#;(¥#,#)');
select *,
wct.TEXT(num_val, num_format)
from #t;
This produces the following result.
{"columns":[{"field":"recno","headerClass":"ag-right-aligned-header","cellClass":"ag-right-aligned-cell"},{"field":"num_val","headerClass":"ag-right-aligned-header","cellClass":"ag-right-aligned-cell"},{"field":"num_format"},{"field":" "}],"rows":[{"recno":"51","num_val":"1.5","num_format":"#.00%"," ":"150.00%"},{"recno":"52","num_val":"-0.75","num_format":"#.00%"," ":"-75.00%"},{"recno":"53","num_val":"1000000","num_format":"$#,#.00"," ":"$1,000,000.00"},{"recno":"54","num_val":"-1000000","num_format":"$#,#.00"," ":"-$1,000,000.00"},{"recno":"55","num_val":"1000000","num_format":"€#,#.00"," ":"€1,000,000.00"},{"recno":"56","num_val":"-1000000","num_format":"€#,#.00;(€#,#.00)"," ":"(€1,000,000.00)"},{"recno":"57","num_val":"1000000","num_format":"¥#,#;(¥#,#)"," ":"¥1,000,000"},{"recno":"58","num_val":"-1000000","num_format":"¥#,#;(¥#,#)"," ":"(¥1,000,000)"}]}