TO_CHAR

Syntax

TO_CHAR
(
  value IN { smallint | int | bigint | real | double precision | numeric | text | timestamp | timestamptz }
  [, fmt IN text]
)
RETURNS text

Overview

The TO_CHAR function converts a number, date, timestamp, or string value to a string according to the format model (fmt) specified.

If no format model is provided, each data type is converted to a string according to its default output format.

For numeric types, integers, floating point numbers, and numeric values are converted according to the format.

Dates and timestamps are converted to the specified format (e.g., 'YYYY-MM-DD', 'HH24:MI:SS').

Parameter

Parameter
Description

value

smallint | int | bigint | real | double precision | numeric | text | timestamp | timestamptz The target value to convert. This can be numbers, dates, timestamps, or string values. For text type, the fmt argument is not available, and you must use the mouth Returns the received string as is.

fmt

text type; It is a format model that specifies the output format. For example, numbers can be specified as ‘FM9999’, and dates/times can be specified as ‘YYYY-MM-DD HH24:MI:SS’.

For supported format models, refer to the PG official documentation.

Example

-- Convert numeric values (without format)
SELECT oracle.TO_CHAR(12345);

 to_char 
---------
 12345
(1 row)

-- Convert numeric values (apply format)
SELECT oracle.TO_CHAR(12345, 'FM0000000');

 to_char 
---------
 0012345
(1 row)

-- Date value conversion (basic format)
SELECT oracle.TO_CHAR(oracle.SYSDATE());

       to_char       
---------------------
 2025-03-07 00:57:44
(1 row)


-- Convert date values (apply format)
SELECT oracle.TO_CHAR(oracle.SYSDATE(), 'DD-MM-YYYY SS:MI:HH24');

       to_char       
---------------------
 07-03-2025 46:58:00
(1 row)

-- Timestamp conversion (timestamptz)
SELECT oracle.TO_CHAR(oracle.SYSTIMESTAMP(), 'YYYY-MM-DD"T"HH24:MI:SS');

       to_char       
---------------------
 2025-03-06T15:59:30
(1 row)

Last updated