// dialect comparison

Implicit casts in comparisons

Four engines convert silently, one refuses, and the conversions differ.

Filed under Querying

The verdict

Write the literal in the column's type and the query behaves the same on all five engines. PostgreSQL is the only engine that refuses the mismatch, which is why code that worked elsewhere breaks there. On the others, a number compared with a text column is either a runtime error waiting for bad data or a full scan.

Where they differ

Compare unlike types and most engines convert one side, each by its own rules. SQL Server, MySQL and Oracle convert the string column to a number; SQLite converts the number to text when the column has TEXT affinity; PostgreSQL raises a type error.

How each engine writes it

SQL Server

SELECT * FROM items WHERE varchar_code = '123';

varchar_code = 123 converts the column to int, because int outranks varchar. One non-numeric value fails the query, and an index on the column can't be used for a seek.

PostgreSQL

SELECT * FROM items WHERE varchar_code = '123';

varchar_code = 123 fails with 'operator does not exist: character varying = integer'. Quote the literal or cast explicitly.

MySQL

SELECT * FROM items WHERE varchar_code = '123';

varchar_code = 123 converts every value to a number ('abc' becomes 0, '12abc' becomes 12) with only a warning, and can't use the index.

SQLite

SELECT * FROM items WHERE varchar_code = '123';

varchar_code = 123 also matches here: the column has TEXT affinity, so SQLite converts 123 to '123' before comparing. Values in a column with no declared type are not converted.

Oracle

SELECT * FROM items WHERE varchar_code = '123';

varchar_code = 123 applies TO_NUMBER to the column, so one row holding 'A12' fails the query with ORA-01722, and a normal index on the column is skipped.

Questions that come up

Why is my indexed varchar column suddenly doing a table scan?

The query compares it with a number, and SQL Server, MySQL and Oracle convert the column side, not the literal. The index holds text, so it can't be searched for converted values. Write the literal as a string: WHERE varchar_code = '123'.

Is MySQL's 'abc' = 0 behavior really silent?

Almost: MySQL converts non-numeric strings to 0 and issues warning 1292 ('Truncated incorrect DOUBLE value'), which most clients never show. So WHERE varchar_code = 0 matches every value that doesn't start with a digit.

How does SQLite decide what equals what?

By affinity. If one operand is a column with INTEGER, REAL or NUMERIC affinity, SQLite tries to convert the other to a number; if it has TEXT affinity, numbers are converted to text. A column declared with no type does no conversion, so '123' and 123 differ there. In sort order, NULL comes first, then numbers, then text, then BLOBs.

Keep reading

// write it once, run it anywhere

Switch engines without relearning SQL

SQLly is a native SQL workbench for macOS, Windows and Linux. Its formatter and plan viewer work in all five dialects.