String Functions

String Functions

String functions perform operations on a string input value and returns a string or other value.

CONTAINS(expression, substring)

True if the string contains the substring.

INITCAP(expression), TITLE(expression)

Converts the string so that the first letter of each word is uppercase and every other letter is lowercase.

LENGTH(expression)

Returns length of the string value.

LOWER(expression)

Returns lowercase of the string value.

LTRIM(expression [, characters ])

Returns string with all leading characters removed. White space is trimmed by default. The function removes leading characters from the expression that match the specified characters and stops when it encounters a character that does not match any of the specified characters.

POSITION(expression, substring)

Returns the first position of the substring within the string, or -1. The position is zero-based, i.e., the first position is 0.

REPEAT(expression, n)

Returns string formed by repeating expression n times.

REPLACE(expression, substring, repl [, n ])

Returns string with all occurrences of substr replaced with repl. If n is given, at most n replacements are performed.

RTRIM(expression, [, characters ])

Returns string with all trailing characters removed. White space is trimmed by default. The function removes trailing characters from the expression that match the specified characters and stops when it encounters a character that does not match any of the specified characters.
select rtrim("2013-06-22 18:33:30 330+0300", "+03") ;
 
    "results": [
        {
            "$1": "2013-06-22 18:33:30 "
        }

select rtrim("2013-06-22 18:33:30 330+0300", " +03") ;
 
    "results": [
        {
            "$1": "2013-06-22 18:33:"
        }

SPLIT(expression [, sep ])

Splits the string into an array of substrings separated by the specified separator sep. If sep is not given, any combination of white space characters is used.

SUBSTR(expression, position [, length ])

Returns substring from the integer position of the given length, or to the end of the string. The position is zero-based, i.e. the first position is 0. If position is negative, it is counted from the end of the string; -1 is the last position in the string.

SUFFIXES(expression)

Since Couchbase Server 4.5

Generates an array of all the suffixes of the input string.

Example

The following example uses the SUFFIXES() function to query the airport names when a partial airport name is given.
Create Index
CREATE INDEX autocomplete_airport_name 
  ON `travel-sample`( DISTINCT ARRAY array_element FOR array_element IN SUFFIXES(LOWER(airportname)) END )
  WHERE type = "airport";
Query
SELECT airportname 
FROM `travel-sample`
WHERE
  ANY array_element IN SUFFIXES(LOWER(airportname)) SATISFIES array_element LIKE 'washing%' END
  AND type="airport";
Results
[
  {
    "airportname": "Washington Dulles Intl"
  },
  {
    "airportname": "Baltimore Washington Intl"
  },
  {
    "airportname": "Ronald Reagan Washington Natl"
  },
  {
    "airportname": "Washington Union Station"
  }
]

The following blog provides more information about this example.

TITLE(expression), INITCAP(expression)

Converts the string so that the first letter of each word is uppercase and every other letter is lowercase.

TRIM(expression [, characters ])

Returns string with all leading and trailing chars removed. White space by default.

UPPER(expression)

Returns uppercase of the string value.

Example

The following example shows the use of a LOWER string function.

SELECT product
FROM product
  UNNEST product.categories as categories
WHERE LOWER(categories) = "appliances"