Insert Text Literal and Concatenation in Oracle SQL

In Oracle, it’s possible to insert literal or concatenate 2 or more charater strings or SQL result output together. This manipulation allows you to manipulate the format of data returned by SQL query.

Two solid vertical bar || operator is used to concatenate 2 or more strings. Beside, Oracle also provides the CONCAT character function as an alternative to the vertical bar operator just in case there is situation where it is difficult or impossible to control translation performed by operating system or network utilities. This function should be used in applications that will be moved between environments with differing character sets.

Oracle will automatically casts values into types which can be concatenated. As Oracle interprets NULL as the empty (zero-length) character string, it doesn’t return NULL if an operand is NULL, meaning concatenating a zero-length character string with another operand always results in the other operand, so null can result only from the concatenation of two null strings. To concatenate an expression that might be null, use the NVL function to explicitly convert the expression to a zero-length string.

Concatenating two strings results in another character string. If both character strings are of datatype CHAR, the result has datatype CHAR and is limited to 2000 characters. If either string is of datatype VARCHAR2, the result has datatype VARCHAR2 and is limited to 4000 characters. Trailing blanks in character strings are preserved by concatenation, regardless of the strings’ datatypes.

For literal insertion, put the strings between the single quote ‘ in the SQL statement.

Example and Usage:

SELECT ‘Name is ‘ || name FROM table;

Name is whatever_name

SELECT number || ‘ – ‘ || description FROM table ORDER BY number

1 – description 1
2 – description 2

Advance Usage:

It’s possible to use Concatenation and Literal Insertion to generate a set of SQL query language automatically, especially when need to perform same operation to lots of tables, i.e dropping a lot of tables. To do this, format the SQL data query language to output the query result in valid SQL format, and spool the SQL query results to a file. Then execute the file that contains SQL statements.

Example:

SELECT ‘DROP TYPE ‘ || type_name || ‘;’

will generates:

DROP TYPE type_name

that can be run at SQL*Plus by calling the file with @filename. All types that been selected from first SQL statements will be dropped from the database.


One Response to “Insert Text Literal and Concatenation in Oracle SQL”

  1. Arun
    March 2nd, 2007 21:57
    1

    Good article. Got what I was looking for…

Leave a Reply

You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Subscribe to comments feature has been disabled. To receive notification of latest comments posted, subscribe to My Digital Life Comments RSS feed or register to receive new comments in daily email digest.
Custom Search

New Articles

Incoming Search Terms for the Article

concatenate in oracle query - sqlplus concatenate string - oracle sql concatenate - sqlplus concat - sql literal - oracle concat text - concatenating in Oracle Sql - oracle query string concat - sqlplus string concatenation - oracle sql concatenation - concatnation string in select query in oracle - literal in sql statement - oracle select concatenate - sql concatenate text oracle - bar security camera with text insert - concat in sql plus - concatenate strings with oracle - concat sql plus - concaténation SQL insert - insert concat value in oracle - insert ||ORA-00996: the concatenate operator is ||, not | - oracle char concatenation - oracle concatenate as - oracle group text concatenation - pl/sql select syntax string concatenation - sql concatenate string oracle - sqlplus concatenate text - sqlplus literal - sql text concatenation, oracle - concatenate character at end of variable sqlplus - concatenation in oracle query - concatenation in oracle select || - concatenation number and varchar in oracle query - concatenation in oracle - include a literal - INSERT text russian in oracle database - INSERT text chinese in oracle - literal in sql - oracle sqlplus concatenate - oracle sql select concatenate - oracle database sql literals - oracle concatenate formatting - oracle litteral & - oracle concatenate returns empty value - oracle concatenation - sqlplus text concatenate - sql insert text - sqlplus concat string - sqlplus concatanate inserts - sql insert ' in string oracle -