Casting Data Types in Postgres
Question: How can I cast a column's data type from varchar to int in a Postgres SELECT statement?
Answer:
Postgres offers several options for casting data types:
Both variants are generally interchangeable, but the second may require additional parentheses in certain scenarios.
Additionally, you can use these syntax variations:
Note that the varchar_col must contain only numerals and an optional leading sign. Leading or trailing whitespace is ignored.
Example:
<code class="sql">SELECT cast(passenger_name AS int), age FROM passengers;</code>
This query will cast the passenger_name column, which is a varchar, to an int before returning the result.
The above is the detailed content of How to Cast Data Types from Varchar to Int in PostgreSQL SELECT Statement?. For more information, please follow other related articles on the PHP Chinese website!