Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

String concatenation operator in Oracle, Postgres and SQL Server

Tags:

Is there a way to have a common operator for concatenation in Oracle, Postgres and SQL Server.

In Oracle and Postgres we use || and SQL Server uses +.

I've solved the problem in Postgres by adding the custom operator + to support string concatenation.

Is there a way to add the same operator in Oracle to support string concatenation using the + operator.

like image 370
Arun P Johny Avatar asked Sep 03 '09 12:09

Arun P Johny


People also ask

What is the concatenation operator in SQL Server?

The concatenation operator is the plus sign (+). You can combine, or concatenate, two or more character strings into a single character string. You can also concatenate binary strings.

What is the concatenation operator in Oracle?

Concatenation operator || In addition to the CONCAT() function, Oracle also provides you with the concatenation operator ( || ) that allows you to concatenate two or more strings in a more readable fashion: string1 || string2 || string3 || ...

How do I concatenate in postgresql?

Concatenation can only be used with character strings. Strings are concatenated by placing the concatenation operator (||) between two or more character strings (string literal or a character string variable) that you wish to be combined.


1 Answers

|| is the SQL Standard concatenation operator (see SQL 2008: 5.2). Use that, and complain if it doesn't work in the system you're using ;-)

Seriously though, you should make other systems use ||, not +. Not only is it more standard, but it's easier to accidentally cause confusion if you use +, especially if any types have to be inferred or and implicit casts are happening.

Consider: '5' + 2

If the system you're using doesn't throw an error on that one, and + means both plus and concatenation, you might be in for some confusing results.

like image 130
Jeff Davis Avatar answered Oct 14 '22 21:10

Jeff Davis