Knowledge base / SQL
CAST in SQL: converting types across database engines
I explain when a type conversion helps and when it can lose information, with each example labelled by engine.

CAST changes a value’s type within a SQL expression. I use it when I need a number rather than text, a date rather than a written date, or an explicit type in a calculation. Before choosing the target, however, I check the engine and input. A shared function name does not mean that every database accepts the same type names or handles information loss identically.
The examples below concern PostgreSQL 18, SQL Server and MySQL 8.4, as labelled beside the code. Values and records are synthetic. I explain the outcomes from documentation; they are not performance measurements or guarantees for other systems.
Syntax and the scope of the change
CAST(expression AS target_type)
In PostgreSQL, CAST('42' AS INTEGER) produces the number 42 because the text is a valid integer representation within the type’s range. PostgreSQL also supports the alternative value::type syntax. I keep CAST here for readability. The value expressions documentation describes both forms.
A cast in SELECT does not change the source column’s definition. Converting a text field for one query leaves the table storing text. A permanent model change is a separate task. Adding casts to several reports therefore does not automatically repair the upstream import process or establish an appropriate type for future data.
Target names depend on the engine
| Goal | PostgreSQL 18 | SQL Server | MySQL 8.4 |
|---|---|---|---|
| Text to integer | CAST('42' AS INTEGER) |
CAST('42' AS INT) |
CAST('42' AS SIGNED) |
| Number to text | CAST(42 AS VARCHAR(10)) |
CAST(42 AS VARCHAR(10)) |
CAST(42 AS CHAR(10)) |
| Text to decimal | CAST('12.50' AS DECIMAL(6,2)) |
CAST('12.50' AS DECIMAL(6,2)) |
CAST('12.50' AS DECIMAL(6,2)) |
The MySQL versions follow its supported CAST target types. Do not copy AS INT or AS VARCHAR(10) into that example from another engine. Numeric literals in the code use a decimal point. Commas in a source file require an agreed parsing convention rather than an arbitrary rewrite of the query’s syntax.
Example: an amount stored as text
Assume a PostgreSQL table called orders with order_id and a text column called amount_text. Two synthetic records contain '12.50' and '7.50'. I can prepare numeric values for later calculations:
SELECT
order_id,
CAST(amount_text AS DECIMAL(6,2)) AS amount
FROM orders;
The resulting numeric amounts are 12.50 and 7.50, with a sum of 20. DECIMAL(6,2) allows six digits in total, including two after the decimal point. It does not mean six digits before it. Check the required range and scale before selecting the type. Larger values may not fit, while additional fractional digits can be rounded according to the engine’s rules.
I would not automatically convert an identifier such as '0012' to a number. Leading zeros may belong to the code rather than represent cosmetic formatting. Establish the field’s meaning first. A technically valid conversion can remove information the recipient needs. Likewise, an empty string should not silently acquire the meaning of zero or a missing amount without an explicit rule.
Choose the numeric type before division
In PostgreSQL, dividing two integers truncates the fractional part. For synthetic values 5 and 2, I therefore select the type before the operation when I need a decimal result:
SELECT CAST(5 AS NUMERIC) / 2 AS ratio;
The value is 2.5, although the client may display additional zeros. Casting an already truncated result cannot recover the lost fraction. The PostgreSQL operator documentation describes integer division. An operand’s type affects the calculation, but its scale does not promise a particular number of decimal places for every subsequent expression.
If I need a particular rounding operation, I write it separately. CAST(10 AS DECIMAL(5,2)) / 3 should not be described as returning exactly 3.33; division may retain more digits. See the ROUND entry for rounding rules and type differences.

Casting to INT is not a universal floor operation
In SQL Server, CAST(125.89 AS INT) gives 125 by truncating the fractional part in this conversion. For negative values, truncation moves towards zero, which differs from mathematical rounding down. Do not generalise that rule to every type and engine. The Microsoft conversion table distinguishes truncating and rounding cases.
When I need explicit truncation in PostgreSQL, I choose trunc; for rounding down, I choose floor. Applied to -2.7, they produce -2 and -3 respectively. These are different goals. Select the operation to match the report’s rule instead of hiding that decision inside a type conversion. Exact and approximate numeric types may also require different assumptions.
A date value is separate from its display
In PostgreSQL, CAST('2024-06-01' AS DATE) creates a date value. It does not establish an arbitrary presentation format or repair every possible input representation. Text dates require a known convention. If a source mixes day and month order, casting alone may not reveal what the original writer intended.
Given orders(order_date_text, amount) with valid ISO date strings, I can select a year using explicit boundaries:
SELECT CAST(order_date_text AS DATE) AS order_date, amount
FROM orders
WHERE CAST(order_date_text AS DATE) >= DATE '2024-01-01'
AND CAST(order_date_text AS DATE) < DATE '2025-01-01';
This is a PostgreSQL example and assumes valid date text. It is not a safe cleaning procedure for an arbitrary dirty source. For the target model, I would consider storing dates as dates so that each report need not repeat the conversion. The DATE_TRUNC entry explains grouping them into periods.
NULL, invalid text and CONVERT
In PostgreSQL, CAST(NULL AS INTEGER) remains NULL, now with a specified type. It does not become zero. A replacement value requires a separate, justified rule; see COALESCE. Missing data and invalid text should also remain distinguishable during import.
SQL Server’s TRY_CAST can return NULL when an allowed conversion fails, but an explicitly prohibited conversion still raises an error. I would preserve the original text and an error indicator rather than subsequently converting every such outcome to zero. Other engines have their own failure rules; do not assume MySQL and PostgreSQL handle invalid inputs identically.
CONVERT also needs an engine label. T-SQL puts the target type first and supports an optional style; MySQL offers its own forms in the same documentation as CAST. Saying that CONVERT exists only in SQL Server would be incorrect. When moving code, check the actual signature, conversion purpose and error behaviour.
An exercise before changing a report
Create a small synthetic source containing a valid amount, a missing value, invalid text and a code with leading zeros. Establish the meaning and expected type of each. Then choose the syntax for your engine and a way to report exceptions. First decide which values may be transformed. Only then can you judge whether the short CAST expression is performing the right operation for your data.
Key takeaways
Does CAST change a column's type in the table?
A cast in SELECT changes the expression's type in the query result. It does not change the source column's definition.
Does casting to INT always truncate the fraction?
Behaviour depends on the engine and source and target types. Use an explicit rounding or truncation operation instead of treating conversion as a universal substitute.
How do I cast text to an integer in MySQL?
For the MySQL 8.4 example, use CAST('42' AS SIGNED). Check supported targets before copying type names from PostgreSQL or T-SQL.
AI was used in an automated way while working on this article. If you want to learn how to do it too, check out this project.
