What is the meaning of the symbol || '-' || in SQL

13

I'm doing a query in an Oracle database and I come across this || '-' || symbology in my query . Would you like to know the meaning?

Select
  xf0cdloc || '-' ||  XN4CDEMP AS LOCOMOTIVA,
From
  Trem
    
asked by anonymous 31.01.2017 / 14:07

3 answers

20

In Oracle and Postgres the two pipes ( || ) are the concatenation operator.

MySQL by default does not use an operator but a function, concat ( ) however it is possible to change it through pipes changed the value of variable sql_mode to PIPES_AS_CONCAT

SELECT 'teste' || 'algo'

Returns a column by concatenating the two values.

Documentation - Oracle

Documentation - Postgres

Documentation - MySQL

    
31.01.2017 / 14:10
10

What your code is doing is selecting two columns from the Trem , xf0cdloc , and XN4CDEMP tables, and you are building a virtual column that will only exist in that query. This virtual column calls LOCOMOTIVA and its content is the concatenation of the contents of xf0cdloc after a dash ( '-' ) and then it already glues the contents of XN4CDEMP .

The || is the concatenation operator. It is defined in the standard SQL specification. But some database systems do not implement it, so this syntax may not work depending on where you use it. Because of this some people recommend using CONCAT() when you want the code to be portable to other systems.

In other systems or other languages would be the equivalent of doing this:

Select
  xf0cdloc + '-' + XN4CDEMP AS LOCOMOTIVA,
From
  Trem
    
31.01.2017 / 14:44
2

    

31.01.2017 / 17:54