Utpal
Utpal

Reputation: 43

How to select a substring up to a character in snowflake?

My table in snowflake has a column with string values. The string has comma and slash. I need to list only the values before first comma or slash. I used snowflake SPLIT function, with that i could only get rid of either of one (comma or slash but not both) character.

Input values:

Compact SUV/Crossover
Luxury Subcompact SUV/Crossover
Subcompact Car,Sports Car
Luxury Car,Luxury Sports Car
Compact Car,Hybrid/Alternative Energy Car
Entrylevel Luxury Car
Midsize Car

Desired output :

Compact SUV
Luxury Subcompact SUV
Subcompact Car
Luxury Car
Compact Car
Entry level Luxury Car
Midsize Car

Upvotes: 1

Views: 2227

Answers (1)

Felipe Hoffa
Felipe Hoffa

Reputation: 59175

A simple regex should help:

select regexp_substr(s, '^[^,/]*')
from (
  select 'Compact Car,Hybrid/Alternative Energy Car' s
)

enter image description here

Upvotes: 2

Related Questions