Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why "||" is used as string concatenation in PostgreSQL/Redshift [closed]

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?

like image 484
Arius Avatar asked Mar 04 '16 13:03

Arius


People also ask

What does || mean in redshift?

|| (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.

What does || do in Postgres?

Description. The PostgreSQL || operator allows you to concatenate 2 or more strings together.

What is the purpose of || in SQL?

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.

Which operator is used for concatenation of string purpose?

The & operator is recommended for string concatenation because it is defined exclusively for strings and reduces your chances of generating an unintended conversion.


1 Answers

-- 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.

like image 83
Joe Harris Avatar answered Sep 25 '22 09:09

Joe Harris