如何在SQL中regex_replace?
How to regex_replace in SQL?
我有一列数据为:
D:\SomeFolder\File1.jpg
D:\SomeFolder\File2.jpg
D:\SomeFolder\File3.jpg
如何使用 SQL 查询替换字符,以便像这样更新列:
E:\DifferentFolder\File1.jpg
E:\DifferentFolder\File2.jpg
E:\DifferentFolder\File3.jpg
您可以尝试使用replace功能。
select replace(val, 'SomeFolder','DifferentFolder') from T
在snowflake中,可以使用
REPLACE( <subject> , <pattern> [ , <replacement> ] )
所以在你的情况下
REPLACE(<column_name>, 'SomeFolder', 'DifferentFolder')
应该可以解决问题。
https://docs.snowflake.com/en/sql-reference/functions/replace.html
如果您想匹配第一个单词,请使用:
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, '(^[^\]+\)([^\]+)\', 'OTHERWORD\', 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
我有一列数据为:
D:\SomeFolder\File1.jpg
D:\SomeFolder\File2.jpg
D:\SomeFolder\File3.jpg
如何使用 SQL 查询替换字符,以便像这样更新列:
E:\DifferentFolder\File1.jpg
E:\DifferentFolder\File2.jpg
E:\DifferentFolder\File3.jpg
您可以尝试使用replace功能。
select replace(val, 'SomeFolder','DifferentFolder') from T
在snowflake中,可以使用
REPLACE( <subject> , <pattern> [ , <replacement> ] )
所以在你的情况下
REPLACE(<column_name>, 'SomeFolder', 'DifferentFolder')
应该可以解决问题。
https://docs.snowflake.com/en/sql-reference/functions/replace.html
如果您想匹配第一个单词,请使用:
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, '(^[^\]+\)([^\]+)\', 'OTHERWORD\', 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 |