Skip to content
Advertisement

How to concatenate strings wrapped around the ‘ character

I would like to concatenate 2 strings/fields from a table into one additional attribute and have it wrapped around the “ ‘ ” character using an sql select statement in oracle.

This statement

select SISTEMA, SUBSISTEMA, SISTEMA||'-'||SUBSISTEMA AS Concatenate 
from VETROCOCONDUTA

produces

SISTEMA SUBSISTEMA Concatenate
"AdZC","Meruge","AdZC-Meruge"

1

select SISTEMA, SUBSISTEMA, SISTEMA||'-'||SUBSISTEMA AS Concatenate 
from VETROCOCONDUTA

valid code but not what I want

2

select SISTEMA, SUBSISTEMA, '||SISTEMA||'-'||SUBSISTEMA||' AS Concatenate 
from VETROCOCONDUTA

Error report – ORA-01722: número inválido

3 Error

select SISTEMA, SUBSISTEMA, ||'||SISTEMA||'-'||SUBSISTEMA||'|| AS Concatenate 
from VETROCOCONDUTA

SQL Error: ORA-00936: falta expressão 00936. 00000 – “missing expression”

I want the code to produce:

SISTEMA SUBSISTEMA Concatenate
"AdZC","Meruge",'AdZC-Meruge'

Advertisement

Answer

You need to concatenate '''' at the start and at the end of the concatenated columns:

select 
  SISTEMA, 
  SUBSISTEMA, 
  '''' || SISTEMA || '-' || SUBSISTEMA  || '''' AS Concatenate 
from VETROCOCONDUTA

See the demo.
Result:

> SISTEMA | SUBSISTEMA | CONCATENATE  
> :------ | :--------- | :------------
> AdZC    | Meruge     | 'AdZC-Meruge'

If there are really double quotes in the columns and you want them removed:

select 
  SISTEMA, 
  SUBSISTEMA, 
  '''' || replace(SISTEMA, '"', '') || '-' || replace(SUBSISTEMA, '"', '')  || '''' AS Concatenate 
from VETROCOCONDUTA

See the demo.
Result:

> SISTEMA | SUBSISTEMA | CONCATENATE  
> :------ | :--------- | :------------
> "AdZC"  | "Meruge"   | 'AdZC-Meruge'
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement