PostgreSQL to_ascii() Function

The PostgreSQL to_ascii() function converts the specified string from a specified encoding to ASCII.

to_ascii() Syntax

This is the syntax of the PostgreSQL to_ascii() function:

to_ascii(string [, encoding])

Parameters

string

Required. The string to convert.

encoding

Optional. The character encoding. It can be an encoding name or a number. Supported encodings include: LATIN1, LATIN2, LATIN9, and WIN1250. The default is the encoding of the current database.

Return value

The PostgreSQL to_ascii() function converts a string string from encoding encoding to ASCII and returns the result.

An error will occur if you provide an unsupported encoding.

to_ascii() Examples

This example shows how to use the to_ascii() function:

SELECT to_ascii('Hello', 'LATIN1');
 to_ascii
----------
 Hello

Let’s look at another example using the wrong encoding:

SELECT to_ascii('Hello', 'UTF8');
ERROR:  encoding conversion from UTF8 to ASCII not supported

Here, PostgreSQL reports an error because the to_ascii() function does not support converting strings from UTF8 encoding to ASCII.