Data Manipulation   «Prev  Next»

Lesson 3 Character functions: CONCAT, LENGTH, INSTR
Objective Describe the use of the character functions CONCAT, LENGTH, and INSTR.

Oracle Character Functions CONCAT, LENGTH, INSTR

Three commonly used string functions are 1) concatenate, 2) length, and 3) instring.
  1. CONCAT or ||: The concatenation function uses different syntax for different programming languages and software. For Oracle, you have two choices:
    • Use two vertical bars (||) to connect the two columns you wish to combine. This is by far the most commonly used variation and you can easily concatenate three columns (or expressions or literal values) together by repeating the vertical bars.
    • Use the CONCAT(a,b) function. This function concatenates the first column with the second column. To add a third column, you must add another CONCAT(a,b) around the first one.

    Here is an example (using the course project) of the first, most commonly used, form of concatenation:
    SELECT CITY || ', ' || STATE || '  ' || ZIP
    FROM CUSTOMER;
    

    The results look like this:
    CITY||','||STATE||''||ZIP
    -----------------------------------
    Omaha, NE  98765
    Makena, HI  99800
    Hollywood, CA  10293
    Kihei, HI  39873	
    
  2. Oracle LENGTH: This simple function tells you the length (in number of characters) of the data in a column, expression, or literal. In the case of VARCHAR2 datatype columns, the defined length of the column is often higher than the actual data stored in each row's column. Use the Oracle length function to count the characters. If a field contains no data (is null) then its length is null.
  3. The Oracle `INSTR` function is a string function used to determine the location of a substring within a string. It returns the position of the specified substring within a larger string, starting from a given position. The function is useful for parsing strings and for situations where you need to know the presence and specific location of a substring within a string.
    Syntax The basic syntax of the `INSTR` function is:
    INSTR(string, substring [, start_position [, occurrence ]])
    
    • `string` is the source string to be searched.
    • `substring` is the sequence of characters to search for within the source string.
    • `start_position` is an optional argument specifying where in the string to start the search. If this is omitted, the default is 1, meaning the search starts at the beginning of the string.
    • `occurrence` is an optional argument that specifies which occurrence of the substring to search for. The default is 1, meaning the function will find the first occurrence of the substring.

    Example Consider the following example:
    SELECT INSTR('HELLO WORLD', 'WOR') AS Position
    FROM DUAL;
    

    In this example, the `INSTR` function searches for the substring `'WOR'` within the string `'HELLO WORLD'`. Since the search starts at the beginning of the string by default and we are looking for the first occurrence also by default, the function will return the position at which the substring `'WOR'` begins.
    Explanation In the string `'HELLO WORLD'`, the substring `'WOR'` starts at position 7. Therefore, the output of the example query will be:
    Position
    --------
    7
    

    This indicates that the substring `'WOR'` was found starting at the 7th character of the string `'HELLO WORLD'`.The `INSTR` function is particularly powerful in data manipulation and analysis tasks, where locating the position of specific patterns within strings is necessary, such as in data cleaning, extraction of specific data segments, or when processing log files and textual data.

The following series of images below shows the syntax for the INSTR function.

1)The syntax of the INSTR function, including parameters. If the character or characters are not found
The diagram above provides the structure of the Oracle `INSTR` function and its parameters. Here's the Oracle script depicted in the image:
INSTR(column, character, start, occurrence)

Parameters:
  1. Column, literal, or expression to evaluate: Specifies the source data where the search occurs.
  2. Character(s) to match: The string or character that is searched for in the specified column or expression.
  3. Starting position for the search (default=position 1): Defines the starting position in the string where the search begins.
  4. Occurrence number to find (default=1 — first occurrence): Indicates the specific occurrence of the character(s) to locate.
1) The syntax of the INSTR function, including parameters. If the character or characters are not found, the INSTR function returns zero.

2) This example shows how to search for the letter a in the LASTNAME column of the CUSTOMER table.
The above diagram shows the usage of the `INSTR` function in Oracle SQL. Here's the script displayed in the image:
SELECT INSTR(FIRSTNAME, 'a')
FROM CUSTOMER;

Explanation:
  • The INSTR function is applied to the FIRSTNAME column from the CUSTOMER table.
  • The function searches for the first occurrence of the character 'a' in the FIRSTNAME column and returns its position.

Sample Output: The output shows the position of the first occurrence of `'a'` in each value of the `FIRSTNAME` column. If the character `'a'` is not found, the function returns `0`.
For example:
  • If FIRSTNAME = "Sarah", the result might be 2 (as 'a' occurs at the second position).
  • If FIRSTNAME = "Tom", the result would be 0 (as 'a' is not present).
2) This example shows how to search for the letter "a" in the LASTNAME column of the CUSTOMER table. The searching used in this function is case-sensitive. In this case, the default starting position and occurrence number were used because these two parameters were omitted.

3) Add a position number as the third parameter to start the search for the character at some point within the field.
3) Add a position number as the third parameter to start the search for the character at some point within the field. For example, if you want to find the word "the" in the LOG_TEXT column, but only in the 20th character or beyond, the query would look like the one shown here. The identical function was added to the WHERE clause to limit the rows returned to those that match the INSTR function.
SELECT INSTR(UPPER(LOG_TEXT), 'FISH', 20) POS,
LOG_TEXT
FROM PET_CARE_LOG
WHERE INSTR(UPPER(LOG_TEXT), 'FISH', 20) > 0 

4) You can find the second, third, or other occurrence of the character by adding a third parameter to the function.
4) You can find the second, third, or other occurrence of the character by adding a third parameter to the function. For example, if you wish to find the second occurrence of the word "the" in the LOG_TEXT column, the query would be revised to include both the starting position (20) and the occurence number (2) as shown here.

5) You can use an INSTR function to set the starting position of another INSTR function. In this example, we have replaced the starting position (20) with an INSTR function that looks for period (.). In effect, we are setting the starting position at the end fo the first sentence in the text.
5) You can use an INSTR function to set the starting position of another INSTR function. In this example, we have replaced the starting position (20) with an INSTR function that looks for period (.). In effect, we are setting the starting position at the end fo the first sentence in the text.

Oracle INSTR Function and Syntax

The INSTR function allows for simple or sophisticated searching through a string for a set of characters, not unlike LTRIM and RTRIM, except that INSTR does not clip anything off. It simply tells you where in the string it found what you were searching for. This is similar to the LIKE logical operator. However, LIKE can only be used in a
  1. where or
  2. having
clause, and INSTR can be used anywhere except in the from clause. Of course, LIKE can be used for complex pattern searches that would be quite difficult, if even possible, using INSTR.
Here is the format for INSTR:
INSTR(string,set [,start [,occurrence ] ])

INSTR searches String


SEMrush Software 3 SEMrush Banner 3