129

So I basically wanna display this (whole row in ONE column):

I like [type column] cake with [icing column] and a [fruit column].

The result should be:

Cake_Column
----------------

I like chocolate cake with whipped_cream and a cherry.

I like strawberry cake with vanilla_cream and a lemon_slice.

etc.

etc.

I need some sort of TO_CHAR statement that does ([column] "some text" [column]) "new_column_name";

What am I supposed to know?

6 Answers 6

179

You have two options for concatenating strings in Oracle:

CONCAT example:

CONCAT(
  CONCAT(
    CONCAT(
      CONCAT(
        CONCAT('I like ', t.type_desc_column), 
        ' cake with '), 
      t.icing_desc_column),
    ' and a '),
  t.fruit_desc_column)

Using || example:

'I like ' || t.type_desc_column || ' cake with ' || t.icing_desc_column || ' and a ' || t.fruit_desc_column
2
  • This is really slow (to run, not really to type). Is there any better way? Oct 23, 2015 at 18:34
  • 2
    This is so ugly, with respect to an very old-brand DBMS. How come Oracle don't suppoer many-argument-version Concat? However, thanks to Shankar, there's a || operator.
    – Scott Chu
    Aug 3, 2017 at 2:31
93

Did you try the || operator ?

Concatenation Operator Documentation from Oracle >>>

50
select 'i like' || type_column || ' with' ect....
1
  • 3
    Thank you so much for this answer. I like the '||' as it makes it easier to maintain the SQL query. Oct 25, 2012 at 0:56
32

Below query works for me @Oracle 10G ----

select PHONE, CONTACT, (ADDR1 ||  '-' || ADDR2 || '-' || ADDR3) as Address
from CUSTOMER_DETAILS
where Code='341'; 

O/P -

1111 abc@gmail.com 4th street-capetown-sa

16

The Oracle/PLSQL CONCAT function allows to concatenate two strings together.

CONCAT( string1, string2 )

string1

The first string to concatenate.

string2

The second string to concatenate.

E.g.

SELECT 'I like ' || type_column_name || ' cake with ' || 
icing_column_name || ' and a ' fruit_column_name || '.' 
AS Cake FROM table;
8

Try this:

SELECT 'I like ' || type_column_name || ' cake with ' || 
icing_column_name || ' and a ' fruit_column_name || '.' 
AS Cake_Column FROM your_table_name;

It should concatenate all that data as a single column entry named "Cake_Column".

Not the answer you're looking for? Browse other questions tagged or ask your own question.