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?

Jeffrey Kemp
  • 59,135
  • 14
  • 106
  • 158
Thundordan
  • 1,299
  • 2
  • 8
  • 4

6 Answers6

180

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
OMG Ponies
  • 325,700
  • 82
  • 523
  • 502
  • This is really slow (to run, not really to type). Is there any better way? – Patrick Szalapski Oct 23 '15 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 03 '17 at 02:31
93

Did you try the || operator ?

Concatenation Operator Documentation from Oracle >>>

AbsoluteƵERØ
  • 7,816
  • 2
  • 24
  • 35
Shankar R10N
  • 4,926
  • 1
  • 21
  • 24
50
select 'i like' || type_column || ' with' ect....
shA.t
  • 16,580
  • 5
  • 54
  • 111
Dani
  • 14,639
  • 11
  • 62
  • 110
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

Rob
  • 4,927
  • 12
  • 49
  • 54
Chaits
  • 339
  • 3
  • 3
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;
Du-Lacoste
  • 11,530
  • 2
  • 71
  • 51
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".

SandPiper
  • 2,816
  • 5
  • 30
  • 52