// dialect comparison

Concatenate a number to a string in SQL Server, PostgreSQL, MySQL, SQLite and Oracle

CONCAT('Order ', 42) runs on all five engines. The operators are where they split.

Filed under Querying

The verdict

CONCAT() is the one spelling that runs on all five. Among the operators, || covers PostgreSQL, SQLite, Oracle and SQL Server 2025; MySQL reads it as OR, and SQL Server before 2025 needs a CAST before +.

Where they differ

The porting trap: 'Order ' || 42 works in PostgreSQL, SQLite, Oracle and SQL Server 2025, but MySQL reads || as logical OR and returns 1. Older SQL Server only has +, and 'Order ' + 42 tries to turn 'Order ' into a number and fails. CONCAT() converts its arguments in every engine.

How each engine writes it

SQL Server

SELECT CONCAT('Order ', 42), 'Order ' + CAST(42 AS VARCHAR(10));

With an int operand, + means addition, so 'Order ' + 42 fails with Msg 245. CONCAT() converts everything and treats NULL operands as empty. SQL Server 2025 and Azure SQL also accept 'Order ' || 42.

PostgreSQL

SELECT 'Order ' || 42;

|| converts the non-text operand to text. 'Order ' + 42 errors, because PostgreSQL tries to read 'Order ' as an integer.

MySQL

SELECT CONCAT('Order ', 42);

CONCAT converts every argument but returns NULL if any argument is NULL. || is logical OR unless sql_mode includes PIPES_AS_CONCAT, and 'Order ' + 42 is numeric addition that returns 42.

SQLite

SELECT 'Order ' || 42;

|| is the only concatenation operator and converts numbers to their text form automatically.

Oracle

SELECT 'Order ' || 42 FROM dual;

|| implicitly converts numbers (and dates, using NLS settings). CONCAT() accepts any number of arguments from 23ai; earlier releases take exactly two, so older code nests the calls.

Questions that come up

Why does 'Order ' + 42 work in MySQL but not SQL Server?

In MySQL + is purely numeric addition: 'Order ' converts to 0 with a warning and the result is 42. No error, but no concatenation either. SQL Server tries to convert 'Order ' to int and raises Msg 245.

How do the operators treat NULL operands?

NULL || 'x' is NULL in PostgreSQL, SQLite and SQL Server 2025, but 'x' in Oracle, which treats NULL as an empty string when concatenating. CONCAT() skips NULLs in SQL Server, PostgreSQL and Oracle but returns NULL in MySQL, and SQL Server's + returns NULL.

What about dates in concatenation?

PostgreSQL's || renders a date in ISO form and Oracle's uses NLS_DATE_FORMAT. SQLite stores dates as text already. SQL Server's + needs a CONVERT first, though CONCAT() accepts dates directly, and MySQL's CONCAT renders the ISO form.

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.