2

I have a column with data as:

D:\SomeFolder\File1.jpg
D:\SomeFolder\File2.jpg
D:\SomeFolder\File3.jpg

How do I replace the characters using a SQL query such that column is updated like this:

E:\DifferentFolder\File1.jpg
E:\DifferentFolder\File2.jpg
E:\DifferentFolder\File3.jpg
5
  • 1
    What's your dbms? Commented Mar 8, 2022 at 7:45
  • I'm using snowflake Commented Mar 8, 2022 at 7:47
  • This doesn't need regex Commented Mar 8, 2022 at 7:48
  • @Kendle what can be used? Commented Mar 8, 2022 at 7:49
  • Do you specifically want to target only the folder named SomeFolder ? Commented Mar 8, 2022 at 7:53

3 Answers 3

1

If you wan to match the first word use:

SELECT column1
    ,REGEXP_REPLACE(column1, '(^[^\\\\]+\\\\)([^\\\\]+)\\\\', '\\1OtherWord\\\\', 1, 1, 'e')
FROM VALUES 
    ('D:\\SomeFolder\\File1.jpg'),
    ('D:\\SomeFolder\\File2.jpg'),
    ('D:\\SAMEFolder\\File2.jpg'),
    ('D:\\SomeFolder\\File3.jpg');
COLUMN1 REGEXP_REPLACE(COLUMN1, '(^[^\\]+\\)([^\\]+)\\', '\1OTHERWORD\\', 1, 1, 'E')
D:\SomeFolder\File1.jpg D:\OtherWord\File1.jpg
D:\SomeFolder\File2.jpg D:\OtherWord\File2.jpg
D:\SAMEFolder\File2.jpg D:\OtherWord\File2.jpg
D:\SomeFolder\File3.jpg D:\OtherWord\File3.jpg
Sign up to request clarification or add additional context in comments.

Comments

1

You can try to use replace function.

select replace(val, 'SomeFolder','DifferentFolder') from T

Comments

1

In snowflake, you can use

REPLACE( <subject> , <pattern> [ , <replacement> ] )

So in your case

REPLACE(<column_name>, 'SomeFolder', 'DifferentFolder') 

should do the trick.

https://docs.snowflake.com/en/sql-reference/functions/replace.html

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.