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

Asked

Viewed 9,451 times

13

I am performing a query in an Oracle database and come across this symbology || '-' ||, in my query. I’d like to know the meaning?

Select
  xf0cdloc || '-' ||  XN4CDEMP AS LOCOMOTIVA,
From
  Trem
  • Can you put at least all the expression that has it? Or the query whole. In this way, it is incomplete.

  • This varies from bank to bank.

  • excuse my ignorance, @rray, I know that the question may appear incomplete... for me this is the "symbol" of concatenation between columns, no? But my question for you is: what can vary from bank to bank? Oo. This will always be concatenation between columns for me.. rs

  • 3

    @Marllonnasser in Sqlserver and Mysql for example the column concatenation is different. In Sqlserver vc concatenates using + for example

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 exchange it for the changed Pipes the variable value sql_mode for PIPES_AS_CONCAT

SELECT 'teste' || 'algo'

Returns a column by concatenating the two values.

Documentation - Oracle

Documentation - Postgres

Documentation - Mysql

10

What your code is doing is selecting two columns from the table Trem, to xf0cdloc and the XN4CDEMP, and is building a virtual column that will only exist in this query. This virtual column calls LOCOMOTIVA and its content is the concatenation of the content of xf0cdloc then a dash ('-') and then already pastes the content 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 the function 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

I put in the Github for future reference.

2

Server For Concaternar String, in Oracle it is possible to concaterate string with number, however in SQL SERVER it is necessary to convert number to string

Browser other questions tagged

You are not signed in. Login or sign up in order to post.