Numeric Conversions: SQL Server

Use the CONVERT function for conversions to and from numeric values:

CONVERT(datatype[(length)], expression[, style])

Table 1-11 lists styles for converting FLOAT and REAL values to character strings. Table 1-12 lists styles for converting MONEY and SMALLMONEY values to character strings.

Table 1-11. SQL Server floating-point styles

Style

Description

0

Default, 0–6 digits, scientific notation when necessary

1

Eight digits + scientific notation

2

16 digits + scientific notation

Table 1-12. SQL Server money styles

Style

Description

0

Money default, no commas, two decimal digits

1

Commas every three digits, two decimal digits

2

No commas, four decimal digits

The following two examples demonstrate numeric conversions using the CONVERT function. The second example combines conversion from text with a monetary conversion:

SELECT CONVERT(VARCHAR(10), 1.234567, 2);

1.234567

SELECT CONVERT(
          VARCHAR,
             CONVERT(MONEY, '20999.95'), 1);

20,999.95

Get SQL Pocket Guide, 3rd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.