SELECT knowledge FROM sql_resources WHERE category='sql' AND slug='strings'

Strings

Introduction

Strings are the most flexible column type, able to encode almost any kind of information. For that reason, they are often used as a last resort when storing a complex object. If that happens, you'll have to crack open this article and get ready to do some slicing and dicing before your analysis can start.

String manipulation functionality and syntax differs between databases. In this article the code snippets are written in the Google BigQuery Standard SQL syntax

String manipulation

The operations listed here transform a string into another string - you'll often need to chain several of these transformations together.

Trimming a string is the act of stripping characters (often whitespace) from the beginning and end of a string. Whitespace is particularly insidious as it is invisible in most query editors, while remaining very much visible to the database while filtering and grouping.

Common trimming function names are TRIM / LTRIM / RTRIM (the latter for only operating on the start and end of strings respectively).

trimmedtrimmed_ltrimmedrtrimmed
hellohellohello------hello

This is the act of adding characters to your string until it reaches the specified length, particularly useful if your database is extra fussy about the format of dates and times. The common function names here are LPAD and RPAD, for adding characters to the beginning and ends of strings respectively.

lpadlpad_2rpad
-hello--hellohello-

Changing case

The common functionality for altering string case are the UPPER / LOWER / INITCAP functions, which do what they say on the tin.

upperlowerinitcap
HELLOhelloHello
palindromenot_a_palindrome
racecaremordnilap

Concatenating

Concatenating, or joining, several strings is a common operation supported with the CONCAT function or sometimes the + or || operators.

concat_functionpipes_operator
hello worldhello world

Analysing strings

The length of a string can be surprising - it depends how you count characters. Most letters you'll see in English can be represented as a single byte, but many others cannot (including all emoji).

Therefore most databases can count either the number of visible characters in a string, or the number of bytes required to represent the string. Common function names are LENGTH / CHAR_LENGTH / BYTE_LENGTH / OCTET_LENGTH / DATALENGTH.

hellohello_bhello_cemojiemoji_bemoji_c
555141

Comparisons

Loading code...

Exact string comparisons are simple - just use the usual = or != comparison operators.

There are however lots more functions for approximate string comparisons, such as STARTS_WITH / ENDS_WITH / REGEXP_CONTAINS / REGEXP_MATCHES / REGEXP_LIKE.

starts_withends_withregexp_contains
truetruetrue

Finally there is the LIKE operator which is another, older way to do approximate string comparisons.

string
a
abc

Substrings

Finding substrings

There are various functions for finding the position of a substring within another string - for example REGEXP_INSTR / STRPOS / POSITION / LOCATE / CHARINDEX.

Loading code...

Make sure you know whether the result is 0-indexed, or 1-indexed - the examples below are 1-indexed.

Loading code...
regexp_instrstrpos
11

Replacing substrings

Useful for cleaning messy string columns, these functions are often called REPLACE or REGEXP_REPLACE. The latter is more flexible as it allows finding substrings using regular expressions.

replaceregexp_replace
Hello??ello?

Extracting substrings

Loading code...

Many functions are available for extracting parts of a string. The most commonly supported are LEFT / RIGHT (for extracting the start and end of a string respectively), and SUBSTR (for extracting a specified chunk of a string).

leftrightsplitsubstrregexp_extractregexp_extract_all
helohello,worldhehehe,lo
Loading code...
Loading code...
Loading code...
Loading code...
Loading code...
Loading code...
Loading code...