PostgreSQL to_number() Function
The PostgreSQL to_number() function converts the specified string to a number according to the given format.
to_number() Syntax
This is the syntax of the PostgreSQL to_number() function:
to_number(str TEXT, format TEXT) -> NUMERIC
Parameters
str-
Required. A string that will to be converted to a number.
format-
Required. The format used for converting this string. You can use the template patterns in the following table to construct numeric format strings.
Pattern Description 9digit position (can be discarded if it doesn’t matter) 0digit position (will not be removed, even if it doesn’t matter) .decimal point ,group (thousands) separator PRnegative values in angle brackets Scurrency symbol (uses locale) LCurrency symbol (uses locale) Ddecimal point (uses locale) Ggroup separator (uses locale) MIminus sign in specified position (if number < 0) PLplus sign in specified position (if number > 0) SGplus/minus sign in specified position RNRoman numerals (between 1 and 3999) TH,thordinal number suffix Vshift specified number of digits EEEEexponent for scientific notation
Return value
The PostgreSQL to_number() function returns a numeric value converted from the specified string according to the specified format.
to_number() Examples
This example shows how to use the PostgreSQL to_number() function to convert a specified string to a numeric value according to the specified format.
SELECT to_number('123,456,789.012-', '999G999G999D999S');
to_number
----------------
-123456789.012