Type Conversion
12 Functions
Type conversion (also known as "casting") is the process of changing a value from one data type to another. This is essential for cleaning data, performing mathematical operations on text-based numbers, and formatting dates for display in your application's UI.
Table of Contents
CAST()
The standard SQL function to convert an expression to a specific data type.
CAST(expression AS target_type)Converting a numeric ID stored as text into an integer so you can sort it numerically.
SELECT CAST('100' AS INTEGER);This is the most "portable" way to cast, meaning the code will work on most other databases (MySQL, SQL Server) if you ever migrate from PostgreSQL.
:: (PostgreSQL Cast)
A PostgreSQL-specific shorthand for the CAST() function.
expression::target_typeQuickly casting a value inside a complex query for better readability.
SELECT total_amount::NUMERIC FROM orders;This is the "Postgres way." It is much more concise than CAST() and is used by almost every PostgreSQL developer for daily tasks.
TO_CHAR()
Converts a timestamp, interval, integer, or numeric value to a string based on a specific format.
TO_CHAR(value, 'format')Formatting a raw price into a currency string (e.g., adding $ sign and 2 decimals).
SELECT TO_CHAR(1250.5, 'L9,999.99');Use this function when the data is leaving the database to be displayed directly on a user's screen or a report.
TO_NUMBER()
Converts a string to a numeric/decimal value using a specified format.
TO_NUMBER(string, 'format')Converting a string like '$1,250.00' back into a number so you can add it to another value.
SELECT TO_NUMBER('1,250.50', '9,999.99');The format string must match the input string's structure (commas, periods) exactly for the conversion to succeed.
TO_DATE()
Converts a string to a date value based on a format.
TO_DATE(string, 'format')Converting a user's text input like '20-Jan-2026' into a proper database Date type.
SELECT TO_DATE('20260120', 'YYYYMMDD');This is safer than using ::DATE because it allows you to handle non-standard date formats explicitly.
TO_TIMESTAMP()
Converts a string or a Unix epoch (seconds) to a full timestamp.
TO_TIMESTAMP(double_precision)Converting a timestamp from a JavaScript API (Unix epoch) into a PostgreSQL format.
SELECT TO_TIMESTAMP(1739274000);This is the standard way to handle "seconds since 1970" data which is common in many web technologies.
TO_JSON() / TO_JSONB()
Converts a SQL value (like a row or an array) into a JSON object.
TO_JSON(expression)Converting a whole table row into a JSON format to be sent to a mobile app.
SELECT TO_JSON(ROW(1, 'XQLora', true));TO_JSONB is generally preferred because it stores data in a binary format, making it faster to search and process later.
ARRAY_TO_STRING()
Converts an array of elements into a single string, separated by a delimiter.
ARRAY_TO_STRING(array, delimiter)Turning a list of tags (stored as an array) into a comma-separated list for a UI badge.
SELECT ARRAY_TO_STRING(ARRAY['SQL', 'Postgres'], ', ');You can also provide a third argument as a replacement for NULL values in the array, e.g., ARRAY_TO_STRING(arr, ', ', 'N/A').
STRING_TO_ARRAY()
The inverse of ARRAY_TO_STRING; splits a string into a PostgreSQL array based on a delimiter.
STRING_TO_ARRAY(string, delimiter)Parsing a CSV-style text column into individual searchable array elements.
SELECT STRING_TO_ARRAY('SQL,Postgres,Data', ',');Combined with UNNEST(), this is a powerful tool for breaking a single comma-separated cell into multiple rows.
CONVERT()
Primarily used in PostgreSQL for converting string encoding (e.g., from UTF-8 to LATIN1).
CONVERT(string, 'src_encoding', 'dest_encoding')Handling legacy data from an old system that uses a different character encoding.
SELECT CONVERT('text_in_utf8', 'UTF8', 'LATIN1');Do not confuse this with SQL Server's CONVERT. In Postgres, use CAST or :: for data type changes, and CONVERT only for character set encoding.
ASCII()
Converts the first character of a string into its integer ASCII code.
ASCII('character')Creating a sorting algorithm based on the raw character codes.
SELECT ASCII('A'); -- Result: 65This is useful for debugging character encoding issues or for creating custom sort orders.
CHR()
The inverse of ASCII(); converts an integer code into its corresponding character.
CHR(integer)Inserting special characters or symbols (like line breaks or tabs) into a text report.
SELECT CHR(65); -- Result: 'A'Use CHR(10) for a newline and CHR(9) for a tab character when building formatted text outputs.