PostgreSQL Tutorial

  • Home
  • Stored Procedures
  • Triggers
  • Views
  • Interfaces
    • PostgreSQL PHP
    • PostgreSQL Python
    • PostgreSQL JDBC
  • Functions
    • Aggregate Functions
    • Date / Time Functions
    • String Functions
    • Math Functions
Home / PostgreSQL String Functions / PostgreSQL TO_NUMBER Function

PostgreSQL TO_NUMBER Function

The PostgreSQL TO_NUMBER() function converts a character string to a numeric value.

Syntax

The syntax of PostgreSQL TO_NUMBER() function is as follows:

1
TO_NUMBER(string, format)

Arguments

The TO_NUMBER() function requires two arguments.

1) string

String to be converted to a number. Its format must be a literal value.

2) format

The format argument indicates how the first argument should be interpreted to create the number.

The following table illustrates the list of valid formats:

FormatDescription
9Numeric value with the specified number of digits
0Numeric value with leading zeros
. (period)decimal point
Ddecimal point that uses locale
, (comma)group (thousand) separator
FMFill mode, which suppresses padding blanks and leading zeroes.
PRNegative value in angle brackets.
SSign anchored to a number that uses locale
LCurrency symbol that uses locale
GGroup separator that uses locale
MIMinus sign in the specified position for numbers that are less than 0.
PLPlus sign in the specified position for numbers that are greater than 0.
SGPlus / minus sign in the specified position
RNRoman numeral that ranges from 1 to 3999
TH or thUpper case or lower case ordinal number suffix

Noted that these format strings also apply to TO_CHAR() function.

Return Value

The TO_NUMBER() function returns a value whose data type is numeric.

Examples

Let’s take a look at some examples of using the TO_NUMBER() function to understand how it works.

A) Convert a string to a number

The following statement converts the string ‘12,345.6-‘ to a number.

1
2
3
4
5
SELECT
    TO_NUMBER(
        '12,345.6-',
        '99G999D9S'
    );

The output is:

1
2
3
4
to_number
-----------
  -12345.6
(1 row)

B) Convert a money amount to a number

The following example shows how to convert a money amount to a number:

1
2
3
4
5
SELECT
    TO_NUMBER(
        '$1,234,567.89',
        'L9G999g999.99'
    );

Here is the result:

1
2
3
4
to_number
-----------
1234567.89
(1 row)

C) Format control

If you don’t specify .99 in the format string, the TO_NUMBER() function will not parse the part after the decimal place.

See the following example:

1
2
3
4
5
SELECT
    TO_NUMBER(
        '1,234,567.89',
        '9G999g999'
    );

It returned 1234567 instead of 1234567.89 as follows:

1
2
3
4
to_number
-----------
   1234567
(1 row)

In this tutorial, you have learned how to use the PostgreSQL TO_NUMBER() function to convert a string to a numeric value.

Previous Tutorial: PostgreSQL TO_CHAR Function

PostgreSQL String Functions

  • ASCII
  • CONCAT
  • CHR
  • FORMAT
  • LEFT
  • LENGTH
  • LPAD
  • MD5
  • POSITION
  • REGEXP_MATCHES
  • REGEXP_REPLACE
  • RIGHT
  • REPLACE
  • SPLIT_PART
  • SUBSTRING
  • TRANSLATE
  • TRIM
  • TO_CHAR
  • TO_NUMBER

About PostgreSQL Tutorial

PostgreSQLTutorial.com is a website dedicated to developers and database administrators who are working on PostgreSQL database management system.

We constantly publish useful PostgreSQL tutorials to keep you up-to-date with the latest PostgreSQL features and technologies. All PostgreSQL tutorials are simple, easy-to-follow and practical.

Recent PostgreSQL Tutorials

  • PostgreSQL ANY Operator
  • PostgreSQL EXISTS
  • How To Delete Duplicate Rows in PostgreSQL
  • PostgreSQL TO_CHAR Function
  • PostgreSQL TO_NUMBER Function
  • PostgreSQL TO_TIMESTAMP Function
  • PostgreSQL CEIL Function
  • PostgreSQL MOD Function
  • PostgreSQL FLOOR Function
  • PostgreSQL ABS Function

More Tutorials

  • PostgreSQL Cheat Sheet
  • PostgreSQL Administration
  • PostgreSQL PHP
  • PostgreSQL Python
  • PostgreSQL JDBC
  • PostgreSQL Resources

Site Info

  • Home
  • About Us
  • Contact Us
  • Privacy Policy

Copyright © 2017 by PostgreSQL Tutorial Website. All Rights Reserved.