I find this really weird. If we will look at the major programming languages they all use "||" as logical "or" operator. Is there any (maybe historical) reason why "||" is living in PostgreSQL along with CONCAT() function?
|| (Concatenation) operatorConcatenates two expressions on either side of the || symbol and returns the concatenated expression. Similar to CONCAT function. For both the CONCAT function and the concatenation operator, if one or both expressions is null, the result of the concatenation is null.
Description. The PostgreSQL || operator allows you to concatenate 2 or more strings together.
The concatenation operator is a binary operator, whose syntax is shown in the general diagram for an SQL Expression. You can use the concatenation operator ( || ) to concatenate two expressions that evaluate to character data types or to numeric data types.
The & operator is recommended for string concatenation because it is defined exclusively for strings and reduces your chances of generating an unintended conversion.
-- DB2 / Oracle / Postgres / ANSI Standard SELECT first_name || ' ' || last_name As full_name FROM customers; -- Sybase / SQL Server / Microsoft Access SELECT FirstName + ' ' + LastName As FullName FROM Customers; -- MySQL SELECT CONCAT(`FirstName`, ' ', `LastName`) As `FullName` FROM `Customers`;
Double pipe concatenation is part of the ANSI SQL standard. SQL was initially developed at IBM deep in the mainframe era. Most of the "major programming languages" you are thinking of did not exist when SQL was created. Most modern languages are "C like" on some level but FORTRAN77, for example, uses //
as it's concatenation operator.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With