Home >Database >Mysql Tutorial >How Can I Cast Data Types in PostgreSQL?
Easily convert a column's data type to other types in a SELECT statement in PostgreSQL. Here’s how:
<code class="language-sql">SELECT cast(varchar_col AS int) FROM table_name;</code>
This syntax complies with SQL standards. Alternatively, PostgreSQL allows the shorthand syntax:
<code class="language-sql">SELECT varchar_col::int FROM table_name;</code>
Both variations can be used almost anywhere, but the second form may require parentheses when nesting.
Additionally, you can use the following syntax variations:
<code class="language-sql">int4(varchar_col) int '123'</code>
Note that int4() uses an internal type name, whereas int '123' expects an untyped quoted string literal. However, int[] '{1,2,3}' must be converted using '{1,2,3}'::int[] or cast('{1,2,3}' AS int[]) .
For more information, please refer to the section on conversion functions and array types in the PostgreSQL documentation.
The above is the detailed content of How Can I Cast Data Types in PostgreSQL?. For more information, please follow other related articles on the PHP Chinese website!