string concatenation operator in oracle postgres and sql server

string concatenation operator in oracle postgres and sql server  using -'sql-server,database,oracle,postgresql'

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

In Oracle we uses '|', postgres uses '||' 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.

asked Oct 13, 2015 by amit_cmps
0 votes

3 Answers

0 votes

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

answered Oct 13, 2015 by manju bhargava
0 votes

You can't overload operators in Oracle. the "+" overload wouldn't work anyway, since Oracle does automatic type conversions ('1'+'1'=2).

The concatenation operator used by Oracle is ||, which is also ANSI-compliant.

There is also the CONCAT function which (as of postgres 9.0 and SQL Server 2012) is supported by all three RDBMSs you need it for.

Note that the Oracle version of CONCAT is not variadic like the other two. If you need to concatenate three or more strings you will need to nest:

answered Oct 13, 2015 by dhananjayksharma
0 votes

'||' certainly works in Oracle, though not apparently SQL Server. (For those who come after us, here's a rosetta stone for SQL: SQL Dialects Reference)

If you're fixing up SQL scripts, I would consider the following solution:


sql-shell-command < sql-file.sql

(sql-file contains '+' operators)


ansi-sql-shell-command < sql-file.sql

sed -e 's/||/\+/' < sql-file.sql | ms-sql-shell-command

(sql-file contains '||' operators, you'd have to convert your files)

The idea is that you start with SQL in one format, and for the special case, you run a filter over it to transform it to the other format. Theoretically, you could turn all '+'es into '||'s, but since a good proportion of those might be numeric-add rather than string-concatenation, that's unlikely to work as well.

The complexity of your filter depends on what you're doing. If you have arbitrary data in your SQL, then you'd have to get it to avoid substituting in strings. But if you're setting up views it'll probably be fine.

You could use the same technique in programs where the SQL is in strings - write a function in the program to turn it from one form to the other.

answered Oct 13, 2015 by vijaygupta1980