starts_with

NAME

starts_with(): Checks whether given text starts with given prefix..

TYPE

Function

DESCRIPTION

A string p is a prefix of s if there is a string t such that CONCAT(p, t) = s.

SYNOPSIS

starts_with(txt TEXT CHARSET utf8, prefix TEXT CHARSET utf8)
  RETURNS INT UNSIGNED

Input:

  • txt: an arbitrary text.
  • prefix: the string suspected/expected to be a prefix as txt.

If txt does indeed start with prefix, starts_with returns the number of characters in the prefix. That is, the length of prefix.

In case of mismatch (not a prefix), the function returns 0.

One should note that a positive number holds in SQL as a TRUE value, whereas 0 holds as FALSE. Also note that in the particular case of the empty string being the prefix, the value 0 is always returned.

EXAMPLES

Trim text (spaces between literals are unaffected):

SELECT starts_with('The quick brown fox', 'The quick') as is_prefix;
+-----------+
| is_prefix |
+-----------+
|         9 |
+-----------+

Similar to the above, quoted for clarity:

SELECT starts_with('The quick brown fox', 'fox') as is_prefix;
+-----------+
| is_prefix |
+-----------+
|         0 |
+-----------+

ENVIRONMENT

MySQL 5.1 or newer

SEE ALSO

get_num_tokens(), split_token()

AUTHOR

Shlomi Noach
 
common_schema documentation