What does SQL Select symbol || mean?
Asked Answered
G

6

75

What does || do in SQL?

SELECT 'a' || ',' || 'b' AS letter
Grosberg answered 29/4, 2014 at 18:31 Comment(3)
See also: #1373738Antimatter
Did you try executing it?Envoi
It doesn't add anything in your example, but is helpful in other contexts, e.g. in ... WHERE Name LIKE '%' || :searched || '%'...Trihydric
S
97

|| represents string concatenation. Unfortunately, string concatenation is not completely portable across all sql dialects:

  • ansi sql: || (infix operator)
  • mysql: concat ( vararg function ). caution: || means 'logical or' (It's configurable, however; thanks to @hvd for pointing that out)
  • oracle: || (infix operator), concat ( caution: function of arity 2 only ! )
  • postgres: || (infix operator)
  • sql server: + (infix operator), concat ( vararg function )
  • sqlite: || (infix operator)

hopefully the confusion is complete ...

Shadrach answered 29/4, 2014 at 18:34 Comment(3)
It's configurable in MySQL.Godfry
so MySQL is the only RDBMS where '||' is equivalent to logical OR?Scleroderma
Also MariaDB treats || as logical OR by default, because MariaDB is a fork of MySQL. They have both changed enough since the fork that they should be considered different products, but they still share this behavior.Priggish
D
9

SELECT 'a' || ',' || 'b' AS letter will combine a letter. The result become 'a,b'

Devonian answered 28/6, 2018 at 6:4 Comment(1)
the || operator concatenates stringsTerm
O
6

It is a concat statement. It will concatenate the two strings.

Here is a helpful post!

What is the difference between "||" operator and concat function in Oracle?

Orva answered 29/4, 2014 at 18:32 Comment(0)
L
5

It's a concatenation operator. So you would get 'a,b' from that. I think || will work on most RDBMS's. SQL Server requires the + operator (thanks to HVD for setting me straight!).

Lanita answered 29/4, 2014 at 18:32 Comment(1)
Microsoft SQL Server is one of the exceptions: it doesn't support ||, and requires +.Godfry
A
5

In Oracle, SQLite3, and MySQL, it concatenates strings. Please see the Oracle documentation. The MySQL documentation.

Also, it's part of ANSI SQL, but read this for more information.

Antimatter answered 29/4, 2014 at 18:32 Comment(0)
P
1

in oracle its a shortcut for concatenate

http://docs.oracle.com/cd/B19306_01/server.102/b14200/operators003.htm

Paphian answered 29/4, 2014 at 18:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.