Skip to main content
SQL Transformation Engine

SQL Type Cast & CONVERT Generator

Quickly generate bulletproof T-SQL, PostgreSQL, MySQL, Oracle, and SQLite type casting statements. Convert string representations, decimals, floats, and timestamps into strict integer types with safe error fallback handlers.

Enter a table column name, literal value, or subquery expression.
Generated SQL Statements
-- Generating query...
Interactive In-Browser Simulation Test
Simulation: (42.85) -> 42 (INT)

Cross-Dialect SQL Cast Cheat Sheet

Direct syntax reference for casting data types to INTEGER across major relational engines:

Database Standard CAST Safe Nullable Cast Shorthand / Alternate
SQL Server (T-SQL) CAST(col AS INT) TRY_CAST(col AS INT) CONVERT(INT, col) / TRY_CONVERT(INT, col)
PostgreSQL CAST(col AS INTEGER) NULLIF(REGEXP_REPLACE(col, '[^0-9]', '', 'g'), '')::INT col::INTEGER or col::BIGINT
MySQL / MariaDB CAST(col AS SIGNED) IF(col REGEXP '^[0-9]+$', CAST(col AS SIGNED), NULL) CONVERT(col, SIGNED)
Oracle PL/SQL CAST(col AS NUMBER) TO_NUMBER(col DEFAULT NULL ON CONVERSION ERROR) TO_NUMBER(col)
SQLite CAST(col AS INTEGER) CASE WHEN typeof(col) = 'integer' THEN col ELSE NULL END col + 0 (Implicit coercion)

Deep Dive: Casting String Values to Integer in SQL Server

In relational database systems like Microsoft SQL Server (T-SQL), converting text data (such as VARCHAR or NVARCHAR) into a numeric integer is one of the most common ETL operations. Whether cleaning user input from web forms, parsing legacy import files, or performing mathematical aggregations on numeric codes, selecting the right casting function is critical for query stability and query optimizer performance.

1. The Difference Between CAST and CONVERT in T-SQL

SQL Server provides two native conversion functions:

  • CAST(expression AS target_type): Standard ANSI SQL. It is portable across various DBMS platforms including PostgreSQL, MySQL, and Oracle.
  • CONVERT(target_type, expression [, style]): Proprietary to SQL Server. While redundant for simple integer casts, it provides an optional style integer parameter essential for formatting date and monetary conversions.

2. Why TRY_CAST is Crucial for Production Pipelines

Traditional CAST('abc' AS INT) aborts the entire transaction with the dreaded Msg 245, Level 16, State 1: Conversion failed when converting the varchar value 'abc' to data type int. Introduced in SQL Server 2012, TRY_CAST and TRY_CONVERT inspect each record safely. If the string contains non-numeric characters, leading symbols, or exceeds the integer boundary (-2,147,483,648 to 2,147,483,647), it automatically evaluates to NULL rather than failing the transaction.

3. Handling Decimal Truncation vs Rounding

When casting a float or decimal like 84.95 to an integer, SQL Server truncates the fractional digits, yielding 84. If business logic requires standard arithmetic rounding (where 84.95 becomes 85), you must wrap the column in ROUND(col, 0) before executing the cast:

SELECT 
    TRY_CAST(ROUND(discount_rate, 0) AS INT) AS rounded_int
FROM sales_records;

Frequently Asked Questions

In SQL Server (T-SQL), use CAST(column_name AS INT) or CONVERT(INT, column_name). For safe conversions that return NULL instead of throwing conversion errors on non-numeric strings, use TRY_CAST(column_name AS INT) or TRY_CONVERT(INT, column_name).
CAST conforms to the ANSI SQL standard and throws a fatal execution error (e.g. Conversion failed when converting the varchar value to data type int) if the value cannot be parsed. TRY_CAST safely catches conversion failures and returns NULL without aborting query execution.
PostgreSQL supports both standard CAST(col AS integer) and the shorthand double-colon operator col::integer. MySQL uses CAST(col AS SIGNED) or CAST(col AS UNSIGNED) for whole numbers because MySQL dialect does not recognize CAST(... AS INT) directly.
Direct integer casting truncates decimal fractional portions. To round before converting, use CAST(ROUND(col, 0) AS INT) or FLOOR()/CEILING() depending on whether you require nearest, lower, or higher integer boundaries.