Data Types and Functions in Snowflake
Jake Roach
Field Data Engineer
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
$$
VARCHAR
, TEXT
, STRING
, etcLEN
is synonymous with LENGTH
Removes leading and trailing characters from text values
$$
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)
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
Chunks text into an array of values based on some separator
$$
ARRAY
$$
$$
<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']
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
SELECT
<field>,
<another-field>,
<third-field>,
CONCAT(
<field>,
<another-field>,
' ',
<third-field>
)
FROM ...;
Can join two or more text values together
$$
,
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