Home >Database >Mysql Tutorial >How to Concatenate Columns and Add Text in Oracle SQL?

How to Concatenate Columns and Add Text in Oracle SQL?

Linda Hamilton
Linda HamiltonOriginal
2025-01-11 09:21:41599browse

How to Concatenate Columns and Add Text in Oracle SQL?

Combining Oracle SQL Columns and Appending Text

Formatting data for specific output often requires combining multiple columns and including extra text. Oracle SQL offers two primary methods: the CONCAT function and the || operator.

Method 1: The CONCAT Function

The CONCAT function's syntax is:

<code class="language-sql">CONCAT(string1, string2, ..., stringN)</code>

Each string can be a literal text string, a column name, or an expression resulting in a string. To combine three columns and add text, use this query:

<code class="language-sql">SELECT CONCAT(
  CONCAT(
    CONCAT(
      CONCAT('I like ', t.type_desc_column), 
      ' cake with '), 
    t.icing_desc_column),
  ' and a '),
  t.fruit_desc_column) AS Cake_Column
FROM table_name t;</code>

Method 2: The || Operator

The || operator provides a more concise way to concatenate strings:

<code class="language-sql">string1 || string2 || ... || stringN</code>

The previous example using || becomes:

<code class="language-sql">SELECT 'I like ' || t.type_desc_column || ' cake with ' || t.icing_desc_column || ' and a ' || t.fruit_desc_column AS Cake_Column
FROM table_name t;</code>

Both CONCAT and || effectively combine columns and add text, enabling customized data presentation.

The above is the detailed content of How to Concatenate Columns and Add Text in Oracle SQL?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn