Tipos de dados e funções no 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 retorna o número de caracteres em um texto
$$
VARCHAR, TEXT, STRING, etc.LEN é sinônimo de LENGTHRemove caracteres no início e no fim do texto
$$
LTRIM e RTRIM como equivalentesSELECT
<field>,
-- Remove caracteres no
-- começo ou fim da coluna
TRIM(<1>, <2>)
FROM ...;
<1>: coluna ou valor a aparar
<2>: opcional, padrão a remover do início/fim; senão, usa ' ' (espaço)
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
Divide o texto em um array de valores usando um separador
$$
ARRAY$$
$$
<1>: coluna para SPLIT
<2>: separador de divisão
SELECT
<field>,
-- Faz SPLIT no campo e usa
-- colchetes para pegar o 1º item
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 collaboratorFROM 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 ...;
Pode juntar dois ou mais textos
$$
,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
Tipos de dados e funções no Snowflake