Enterprise Edition Home | Express Edition Home | Previous Page | Next Page   Data Types and Expressions > Expression >

String-Manipulation Functions

String-manipulation functions perform various operations on strings of characters. The syntax for string-manipulation functions is as follows.

Read syntax diagramSkip visual syntax diagramString-Manipulation Functions:
 
                       (1)
|--+-| TRIM Function |-------------------------+----------------|
   |                        (2)                |
   +-| SUBSTRING Function |--------------------+
   |  (3)                       (4)            |
   +--------| SUBSTR Function |----------------+
   |  (3)                        (5)           |
   +--------| REPLACE Function |---------------+
   |  (3)                     (6)              |
   +--------| LPAD Function |------------------+
   |  (3)                     (7)              |
   +--------| RPAD Function |------------------+
   |  (3)                                 (8)  |
   '--------| Case-Conversion Functions |------'
 

Notes:
  1. See page TRIM Function
  2. See page SUBSTRING Function
  3. Informix extension
  4. See page SUBSTR Function
  5. See page REPLACE Function
  6. See page LPAD Function
  7. See page RPAD Function
  8. See page Case-Conversion Functions

TRIM Function

The TRIM function removes leading or trailing pad characters from a string.

Read syntax diagramSkip visual syntax diagramTRIM Function:
 
|--TRIM--------------------------------------------------------->
 
>--(--+-----------------------------------------+--source_expression)--|
      | .-BOTH-----.                            |
      '-+-TRAILING-+--+-----------------+--FROM-'
        '-LEADING--'  '-trim_expression-'
 

Element Description Restrictions Syntax
trim
_expression
Expression that evaluates to a single character or NULL. Default is a blank space ( = ASCII 32) Must be a character expression Quoted String, p. Quoted String
source
_expression
Character expression, including a character column name, or a call to another TRIM function Cannot be LVARCHAR nor a host variable Quoted String, p. Quoted String

The TRIM function returns a VARCHAR value identical to its character string argument, except that any leading or trailing whitespace characters, if specified, are deleted. If no trim qualifier (LEADING, TRAILING, or BOTH) is specified, BOTH is the default. If no trim_expression is used, a single blank space is assumed. If either the trim_expression or the source_expression evaluates to null, the result of the TRIM function is NULL. The maximum length of the returned string must be 255 bytes or fewer, because the VARCHAR data type supports no more than 255 bytes.

The following example shows some generic uses for the TRIM function:

SELECT TRIM (c1) FROM tab;
SELECT TRIM (TRAILING '#' FROM c1) FROM tab;
SELECT TRIM (LEADING FROM c1) FROM tab;
UPDATE c1='xyz' FROM tab WHERE LENGTH(TRIM(c1))=5;
SELECT c1, TRIM(LEADING '#' FROM TRIM(TRAILING '%' FROM
   '###abc%%%')) FROM tab;

When you use the DESCRIBE statement with a SELECT statement that uses the TRIM function in the projection list, the described character type of the trimmed column depends on the database server that you are using and on the data type of the source_expression. For further information on the GLS aspects of the TRIM function in ESQL/C, see the IBM Informix GLS User's Guide.

Fixed Character Columns

The TRIM function can be specified on fixed-length character columns. If the length of the string is not completely filled, the unused characters are padded with blank space. Figure 5 shows this concept for the column entry '##A2T##', where the column is defined as CHAR(10).

Figure 5. Column Entry in a Fixed-Length Character Column
begin figure description - This figure is described in the surrounding text. - end figure description

If you want to trim the sharp sign ( # ) trim_expression from the column, you need to consider the blank padded spaces as well as the actual characters.

For example, if you specify the trim specification BOTH, the result from the trim operation is A2T##, because the TRIM function does not match the blank padded space that follows the string. In this case, the only sharp signs ( # ) trimmed are those that precede the other characters. The SELECT statement is shown, followed by Figure 6, which presents the result.

SELECT TRIM(LEADING '#' FROM col1) FROM taba;

Figure 6. Result of TRIM Operation
begin figure description - This figure is described in the surrounding text. - end figure description

This SELECT statement removes all occurrences of the sharp ( # ) sign:

SELECT TRIM(BOTH '#' FROM TRIM(TRAILING ' ' FROM col1)) FROM taba;

SUBSTRING Function

The SUBSTRING function returns a subset of a character string.

Read syntax diagramSkip visual syntax diagramSUBSTRING Function:
 
|--SUBSTRING--(--source_string--FROM--start_position--+-------------+--)--|
                                                      '-FOR--length-'
 

Element Description Restrictions Syntax
length Number of characters to return from source_string Must be an expression, constant, column, or host variable that returns an integer Literal Number, p. Literal Number
source_string String argument to the SUBSTRING function Must be an expression, constant, column, or host variable whose value can be converted to a character data type Expression,
p. Expression
start_position Position in source_string of first returned character Must be an expression, constant, column, or host variable that returns an integer Literal Number, p. Literal Number

Any argument to the SUBSTRING function must be of a built-in data type.

The subset begins at the column position that start_position specifies. The following table shows how the database server determines the starting position of the returned subset based on the input value of the start_position.

Value of Start_Position How the Database Server Determines the
Starting Position of the Return Subset
Positive

Counts forward from the first character in source_string

For example, if start_position = 1, the first character in the source_string is the first character in the returned subset.

Zero (0)

Counts from one position before (that is, to the left of) the first character in source_string

For example, if start_position = 0 and length = 1, the database server returns NULL, whereas if length = 2, the database server returns the first character in source_string.

Negative

Counts backward from one position after (that is, to the right of) the last character in source_string

For example, if start_position = -1, the starting position of the returned subset is the last character in source_string.

In locales for languages with a right-to-left writing direction, such as Arabic, Farsi, or Hebrew, right should replace left in the preceding table.

The size of the subset is specified by length. The length parameter refers to the number of logical characters, rather than to the number of bytes. If you omit the length parameter, or if you specify a length that is greater than the number of characters from start_position to the end of source_string, the SUBSTRING function returns the entire portion of source_ string that begins at start_position. The following example specifies that the subset of the source string that begins in column position 3 and is two characters long should be returned:

SELECT SUBSTRING('ABCDEFG' FROM 3 FOR 2) FROM mytable

The following table shows the output of this SELECT statement.

(constant)
CD

In the following example, the user specifies a negative start_position for the return subset:

SELECT SUBSTRING('ABCDEFG' FROM -3 FOR 7)
   FROM mytable

The database server starts at the -3 position (four positions before the first character) and counts forward for 7 characters. The following table shows the output of this SELECT statement.

(constant)
ABC

SUBSTR Function

The SUBSTR function has the same purpose as the SUBSTRING function (to return a subset of a source string), but it uses different syntax.

Read syntax diagramSkip visual syntax diagramSUBSTR Function:
 
|--SUBSTR--(--source_string--,--start_position--+-----------+--)--|
                                                '-,--length-'
 

Element Description Restrictions Syntax
length Number of characters to be returned from source_string Must be an expression, literal, column, or host variable that returns an integer Expression, p. Expression
source_string String that serves as input to the SUBSTR function Must be an expression, literal, column, or host variable of a data type that can be converted to a character data type Expression, p. Expression
start_position Column position in source_string where the SUBSTR function starts to return characters Must be an integer expression, literal, column, or host variable. Can have a plus sign ( + ), a minus sign ( - ), or no sign. Literal Number,
p. Literal Number

Any argument to the SUBSTR function must be of a built-in data type.

The SUBSTR function returns a subset of source_string. The subset begins at the column position that start_position specifies. The following table shows how the database server determines the starting position of the returned subset based on the input value of the start_position.

Value of Start_Position How the Database Server Determines the
Starting Position of the Returned Subset
Positive Counts forward from the first character in source_string
Zero (0) Counts forward from the first character in source_string (that is, treats a start_position of 0 as equivalent to 1)
Negative Counts backward from an origin that immediately follows the last character in source_string A value of -1 returns the last character in source_string.

The length parameter specifies the number of logical characters (not the number of bytes) in the subset. If you omit the length parameter, the SUBSTR function returns the entire portion of source_string that begins at start_position.

If you specify a negative start_position whose absolute value is greater than the number of characters in source_string, or if length is greater than the number of characters from start_position to the end of source_string, SUBSTR returns NULL. (In this case, the behavior of SUBSTR is different from that of the SUBSTRING function, which returns all the characters from start_position to the last character of source_string, rather than returning NULL.)

The next example specifies that the string of characters to be returned begins at a starting position 3 characters before the end of a 7-character source_string. This implies that the starting position is the fifth character of source_string. Because the user does not specify a value for length, the database server returns a string that includes all characters from character-position 5 to the end of source_string.

SELECT SUBSTR('ABCDEFG', -3)
   FROM mytable

The following table shows the output of this SELECT statement.

(constant)
EFG

REPLACE Function

The REPLACE function replaces specified characters within a source string with different characters.

Read syntax diagramSkip visual syntax diagramREPLACE Function:
 
|--REPLACE--(--source_string--,--old_string--+---------------+--)--|
                                             '-,--new_string-'
 

Element Description Restrictions Syntax
new_string Character or characters that replace old_string in the return string Must be an expression, constant, column, or host variable of a data type that can be converted to a character data type Expression, p. Expression
old_string Character or characters in source_string that are to be replaced by new_string Must be an expression, constant, column, or host variable of a data type that can be converted to a character data type Expression, p. Expression
source_string String of characters argument to the REPLACE function Must be an expression, constant, column, or host variable of a data type that can be converted to a character data type Expression, p. Expression

Any argument to the REPLACE function must be of a built-in data type.

The REPLACE function returns a copy of source_string in which every occurrence of old_string is replaced by new_string. If you omit the new_string option, every occurrence of old_string is omitted from the return string.

In the following example, the user replaces every occurrence of xz in the source string with t:

SELECT REPLACE('Mighxzy xzime', 'xz', 't')
   FROM mytable

The following table shows the output of this SELECT statement.

(constant)
Mighty time

LPAD Function

The LPAD function returns a copy of source_string that is left-padded to the total number of characters specified by length.

Read syntax diagramSkip visual syntax diagramLPAD Function:
 
|--LPAD--(--source_string--,--length--+---------------+--)------|
                                      '-,--pad_string-'
 

Element Description Restrictions Syntax
length Integer value that specifies total number of characters in the returned string Must be an expression, constant, column, or host variable of a data type that can be converted to a character data type Literal Number,
p. Literal Number
pad_string String that specifies the pad character or characters Must be an expression, constant, column, or host variable of a data type that can be converted to a character data type Expression, p. Expression
source_string String that serves as input to the LPAD function Must be an expression, constant, column, or host variable of a data type that can be converted to a character data type Expression, p. Expression

Any argument to the LPAD function must be of a built-in data type.

The pad_string parameter specifies the character or characters to be used for padding the source string. The sequence of pad characters occurs as many times as necessary to make the return string the length specified by length.

The series of pad characters in pad_string is truncated if it is too long to fit into length. If you specify no pad_string, the default value is a single blank.

In the following example, the user specifies that the source string is to be left-padded to a total length of 16 characters. The user also specifies that the pad characters are a series consisting of a hyphen and an underscore ( -_ ).

SELECT LPAD('Here we are', 16, '-_') FROM mytable

The following table shows the output of this SELECT statement.

(constant)
-_-_-Here we are

RPAD Function

The RPAD function returns a copy of source_string that is right-padded to the total number of characters that length specifies.

Read syntax diagramSkip visual syntax diagramRPAD Function:
 
|--RPAD--(--source_string--,--length--+---------------+--)------|
                                      '-,--pad_string-'
 

Element Description Restrictions Syntax
length The number of characters in the returned string Must be an expression, constant, column, or host variable that returns an integer Literal Number,
p. Literal Number
pad_string String that specifies the pad character or characters Must be an expression, column, constant, or host variable of a data type that can be converted to a character data type Expression,
p. Expression
source_string String that serves as input to the RPAD function Same as for pad_stringe Expression,
p. Expression

Any argument to the RPAD function must be of a built-in data type.

The pad_string parameter specifies the pad character or characters to be used to pad the source string.

The series of pad characters occurs as many times as necessary to make the return string reach the length that length specifies. The series of pad characters in pad_string is truncated if it is too long to fit into length. If you omit the pad_string parameter, the default value is a single blank space.

In the following example, the user specifies that the source string is to be right-padded to a total length of 18 characters. The user also specifies that the pad characters to be used are a sequence consisting of a question mark and an exclamation point ( ?! )

SELECT RPAD('Where are you', 18, '?!')
   FROM mytable

The following table shows the output of this SELECT statement.

(constant)
Where are you?!?!?
Enterprise Edition Home | Express Edition Home | [ Top of Page | Previous Page | Next Page | Contents | Index ]