Manipulating text data

Data Types and Functions in Snowflake

Jake Roach

Field Data Engineer

Finding the LENGTH of a string

LENGTH(<field>)
SELECT
    song_name,
    LENGTH(song_name) AS characters
FROM MUSIC.songs;
       song_name    |  characters
   ---------------- | -----------
     Levon          |      5     
     Tiny Dancer    |      11
     Rocket Man     |      10

LENGTH returns the numbers of characters in a string of text

$$

  • Includes spaces
  • VARCHAR, TEXT, STRING, etc
  • LEN is synonymous with LENGTH
Data Types and Functions in Snowflake

TRIM

Removes leading and trailing characters from text values

$$

  • Most commonly used to remove spaces
  • Case-insensitive
  • Has counterparts LTRIM and RTRIM
SELECT
    <field>,

    -- Remove characters at the
    -- beginning or end of the column
    TRIM(<1>, <2>)

FROM ...;

<1>: the column or value that will be trimmed

<2>: optional, the pattern to trim from the start or end, otherwise, it's a ' ' (space)

Data Types and Functions in Snowflake

TRIM

SELECT
    song_long_name,
    TRIM(song_long_name, '(Remastered)') AS trimmed_song_name
FROM MUSIC.songs;
TRIM(song_long_name, '(Remastered)')
                song_long_name          |     trimmed_song_name
           ---------------------------- | --------------------------
             (Remastered) Piano Man     |      Piano Man
             Ticking (Remastered)       |      Ticking
             Come Sail Away             |      Come Sail Away
Data Types and Functions in Snowflake

SPLIT

Chunks text into an array of values based on some separator

$$

  • Return type is ARRAY
  • Can use bracket-notation to retrieve values

$$

$$

<1>: the column to SPLIT

<2>: the separator to split by

SELECT
    <field>,

    -- SPLIT the field, use bracket-
    -- notation to retrieve first chunk
    SPLIT(<1>, <2>),
    SPLIT(<1>, <2>)[X]

FROM ...;
       Math,Science,Art,Reading
                 ...
 ['Math', 'Science', 'Art', 'Reading']
Data Types and Functions in Snowflake

SPLIT

SELECT
    collaborators,

SPLIT(collaborators, ',') AS all_collaborators,
SPLIT(collaborators, ',')[0] AS primary_artist -- Return the first collaborator
FROM MUSIC.songs;
          collaborators       |          all_collaborators         |  primary_artist
   -------------------------- | ---------------------------------- | ---------------
   Queen, David Bowie         |  ['Queen', ' David Bowie']         |  Queen
   Elton John, Kiki Dee       |  ['Elton John', ' Kiki Dee']       |  Elton John
   Carly Simon, James Taylor  |  ['Carly Simon', ' James Taylor']  |  Carly Simon
Data Types and Functions in Snowflake

CONCAT

SELECT
    <field>,
    <another-field>,
    <third-field>,

    CONCAT(
        <field>, 
        <another-field>,
        ' ',
        <third-field>
    )

FROM ...;

Can join two or more text values together

$$

  • Arbitrary number of values, separated by ,
  • Need to specify spaces between characters
  • Useful for combining first and last names
Data Types and Functions in Snowflake

CONCAT

SELECT
    song_name, artist_name,

    -- Concatenate three text values together
    CONCAT(song_name, ' is written by ', artist_name) AS description

FROM MUSIC.songs;
      song_name    |   artist_name  |         description        
 ----------------- | -------------- | --------------------------------------------
 Night Moves       |  Bob Seger     |  Night Moves is written by Bob Seger
 Cracklin' Rosie   |  Neal Diamond  |  Cracklin' Rosie is written by Neal Diamond
Data Types and Functions in Snowflake

Let's practice!

Data Types and Functions in Snowflake

Preparing Video For Download...