// math reference

SQL implicit numeric conversion

How each engine treats string operands in arithmetic: lenient prefix parsing on MySQL and SQLite, strict conversion on SQL Server and Oracle, and no coercion of text-typed values on PostgreSQL.

Categoría
Math
Returns
numeric or error
In this category
78 of 82

Sintaxis

'1' + 1
CAST('1' AS INT) + 1

Each block is a separate form. The support table and the examples show which engine takes which.

Soporte del motor

MotorSupportNotas
SQL Server Different name or syntax Converts the string to the number's type: '1' + 1 is 2, but '1.5' + 1 fails because '1.5' is not a valid INT. 'abc' raises Msg 245
PostgreSQL Different name or syntax An untyped literal '1' resolves to integer, so '1' + 1 is 2; a text column or '1'::text + 1 raises 'operator does not exist: text + integer'
MySQL Sí Coerces: '1' + 1 is 2; non-numeric text coerces to 0 with a warning
SQLite Sí Coerces: '1' + 1 is 2; non-numeric text becomes 0, so 'abc' + 1 is 1
Oracle Different name or syntax Implicit TO_NUMBER on numeric-looking strings: '1' + 1 is 2; non-numeric text raises ORA-01722

Ejemplos

MySQL

SELECT '1' + 1 AS coerced, 'abc' + 1 AS zero_coercion;  -- 2, 1

SQLite

SELECT '12abc' + 1 AS prefix_coercion;  -- 13

PostgreSQL

SELECT '1' + 1 AS literal_ok, '1'::text::int + 1 AS cast_needed;  -- 2, 2; '1'::text + 1 is a type error

SQL Server

SELECT '1' + 1 AS ok;  -- 2, while '1.5' + 1 raises Msg 245

Conviene saberlo

  • MySQL and SQLite are the loosest: text with no leading number becomes 0, so typos become zeros instead of errors, and '12abc' coerces as 12. MySQL at least records a truncation warning.
  • SQL Server converts the string to the other operand's type, so '1.5' + 1 fails ('1.5' is not an INT) while '1.5' + 1.0 works. PostgreSQL accepts the literal '1' + 1 but rejects text_col + 1; cast explicitly: text_col::int + 1.
  • Oracle's ORA-01722 on bad data fires mid-query depending on plan order: a WHERE that worked on ten rows can fail on ten thousand.

Questions about coercion

Is '1' + 1 valid SQL?

As a literal, yes on all five: it returns 2. The differences start with real data: SQL Server fails on '1.5' + 1, PostgreSQL rejects any text-typed column in arithmetic, Oracle raises ORA-01722 on bad strings, and MySQL and SQLite quietly turn garbage into 0. Cast explicitly.

What does 'abc' + 1 return on SQLite?

1: SQLite coerces non-numeric text to 0 (and '12abc' to 12) rather than erroring. Handy for parsing, dangerous for catching bad data.

Why does my Oracle query raise ORA-01722 intermittently?

Implicit TO_NUMBER conversion is applied per row at the plan's whim: a string column holding one non-numeric value fails when the scan order reaches it. Cast explicitly or clean the column.

Browse every function in the SQL function reference, or see what the SQLly desktop app does for your database.

Cheat sheets for the engines that have it: SQL Server, PostgreSQL, MySQL, SQLite, Oracle.

// fewer lookups

Functions your engine actually has

SQLly's completion lists the built-in functions of the engine you're connected to, whether that's SQL Server, PostgreSQL, MySQL, SQLite or Oracle. Hover a function for help, and signature hints show each argument as you type.