Manipular texto

Tipos de datos y funciones en Snowflake

Jake Roach

Field Data Engineer

Calcular la LENGTH de un texto

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 devuelve el número de caracteres de un texto

$$

  • Incluye espacios
  • VARCHAR, TEXT, STRING, etc.
  • LEN es sinónimo de LENGTH
Tipos de datos y funciones en Snowflake

TRIM

Elimina caracteres iniciales y finales de textos

$$

  • Se usa sobre todo para quitar espacios
  • No distingue mayúsculas/minúsculas
  • Tiene equivalentes: LTRIM y RTRIM
SELECT
    <field>,

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

FROM ...;

<1>: la columna o valor a recortar

<2>: opcional, el patrón a quitar del inicio o final; si no, es un ' ' (espacio)

Tipos de datos y funciones en 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
Tipos de datos y funciones en Snowflake

SPLIT

Divide el texto en un array de valores según un separador

$$

  • El tipo devuelto es ARRAY
  • Puedes usar corchetes para recuperar valores

$$

$$

<1>: la columna a SPLIT

<2>: el separador

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']
Tipos de datos y funciones en 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
Tipos de datos y funciones en Snowflake

CONCAT

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

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

FROM ...;

Puede unir dos o más textos

$$

  • Número arbitrario de valores, separados por ,
  • Debes indicar los espacios entre caracteres
  • Útil para combinar nombre y apellido
Tipos de datos y funciones en 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
Tipos de datos y funciones en Snowflake

¡Vamos a practicar!

Tipos de datos y funciones en Snowflake

Preparing Video For Download...