Oracle SQL, concatenate multiple columns + add text

SqlOracleConcatenation

Sql Problem Overview


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?

Sql Solutions


Solution 1 - Sql

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

Solution 2 - Sql

Solution 3 - Sql

select 'i like' || type_column || ' with' ect....

Solution 4 - Sql

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

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

O/P - > 1111 [email protected] 4th street-capetown-sa

Solution 5 - Sql

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;

Solution 6 - Sql

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionThundordanView Question on Stackoverflow
Solution 1 - SqlOMG PoniesView Answer on Stackoverflow
Solution 2 - SqlShankar R10NView Answer on Stackoverflow
Solution 3 - SqlDaniView Answer on Stackoverflow
Solution 4 - SqlChaitsView Answer on Stackoverflow
Solution 5 - SqlDulacosteView Answer on Stackoverflow
Solution 6 - SqlSandPiperView Answer on Stackoverflow