String Functions and Operators

Applies to: ✅ Data 360 SQL ✅ Tableau Hyper API

Functions and operators for examining and manipulating string values. Use string functions to:

  • Transform case by converting strings to uppercase, lowercase, or title case.
  • Extract and search substrings, find positions, and split text.
  • Pad and trim strings to adjust length and remove unwanted characters.
  • Encode and hash data for storage or security purposes.

Considerations 

  • Character and byte length: Functions like char_length count characters, but octet_length counts bytes. This distinction matters for multi-byte UTF-8 characters.
  • 1-based indexing: String positions start at 1, not 0.
  • NULL handling: The || operator returns NULL if either operand is NULL. Use concat to ignore NULL values.

For more information about supported types, see String Types.

Concatenation 

Combine strings and values:

Length and Measurement 

Measure string size:

Case Conversion 

Transform string case:

  • LOWER — Convert to lowercase.
  • UPPER — Convert to uppercase.
  • INITCAP — Capitalize the first letter of each word.

Substring Operations 

Extract and locate substrings:

  • SUBSTRING — Extract a substring by position and length.
  • SUBSTR — Extract a substring (alternative syntax).
  • LEFT — Get the first n characters.
  • RIGHT — Get the last n characters.
  • POSITION — Find the location of a substring.
  • STRPOS — Find substring location (alternative syntax).
  • OVERLAY — Replace a substring at a position.
  • SPLIT — Split a string into an array by delimiter.
  • SPLIT_PART — Split a string on delimiter and return a field.

Trimming and Padding 

Adjust string length:

  • TRIM — Remove characters from start, end, or both.
  • BTRIM — Remove characters from both ends.
  • LTRIM — Remove characters from the start.
  • RTRIM — Remove characters from the end.
  • LPAD — Pad a string on the left.
  • RPAD — Pad a string on the right.
  • SPACE — Generate a string of spaces.

String Modification 

Transform and manipulate strings:

  • REPLACE — Replace all occurrences of a substring.
  • REPEAT — Repeat a string multiple times.
  • REVERSE — Reverse the characters in a string.

Search and Match 

Check for substrings and string boundaries:

Character Conversion 

Convert between characters and codes:

  • ASCII — Get the ASCII code of the first character.
  • CHR — Get the character for an ASCII code.

Encoding and Hashing 

Encode data and compute hashes:

  • ENCODE — Encode binary data to text (base64, hex, escape).
  • DECODE — Decode text to binary data.
  • TO_BASE64 — Convert data to base64.
  • TO_HEX — Convert data to hexadecimal.
  • MD5 — Compute the MD5 hash for data.
  • SHA256 — Compute the SHA-256 hash for data.

Quoting 

Prepare strings for SQL statements:

Examples 

Data Cleaning 

Normalize and clean user input.

1SELECT
2    trim(both ' ' from input_text) AS trimmed,
3    lower(trim(both ' ' from input_text)) AS normalized
4FROM user_input;

Text Parsing 

Extract components from structured strings.

1SELECT
2    split_part(email, '@', 1) AS username,
3    split_part(email, '@', 2) AS domain
4FROM users;

String Formatting 

Format data for display.

1SELECT
2    initcap(first_name) || ' ' || initcap(last_name) AS full_name,
3    lpad(employee_id::text, 6, '0') AS padded_id
4FROM employees;

Search and Replace 

Find and modify text patterns.

1SELECT
2    replace(description, 'old_term', 'new_term') AS updated_description
3FROM products
4WHERE position('old_term' in description) > 0;

Related Documentation