Extract substring from file path in snowflake

597 views Asked by At

I need to extract substring from list of file paths. Paths are like "\rootfolder\subfolder". Need extract substring as "rootfolder" ( between 2nd backslash and 3rd backslash.

select SUBSTR('\\rootfolder\subfolder\', 3, (REGEXP_INSTR('\\rootfolder\subfolder\','\', 1, 3) - 3)) from table;

But this expression didn't give right result in Snowflake. problem is with backslash character.

1

There are 1 answers

2
Lukasz Szozda On

Using SPLIT_PART:

SELECT SPLIT_PART($$\\rootfolder\subfolder\$$, $$\$$, 3);
--
SELECT SPLIT_PART('\\\\rootfolder\\subfolder\\', '\\', 3);

Output:

enter image description here

Dollar quoting was used to avoid doubling \.