2

我正在尝试将反向引用的字符串小写,但它不起作用。:(我只想降低root之后的第一个字符。

my_string = "<root>Here is July's best food</root>"

正确的应该是:

my_string = "<root>here is July's best food</root>"

这是我的查询:

UPDATE my_table
SET my_string = regexp_replace(my_string, '<root>(.)', lower(E'<root>\\1'))
WHERE my_id = 1;
4

1 回答 1

1

使用split_part()lower()

 select '<root>'||lower(split_part(split_part('<root>Here is July''s best food</root>',' ',1),'>',2)) || split_part('<root>Here is July''s best food</root>',split_part(split_part('<root>Here is July''s best food</root>',' ',1),'>',2),2)

结果:

 "<root>here is July's best food</root>"

UPDATE my_table
SET my_string =  '<root>'||lower(split_part(split_part(my_string,' ',1),'>',2)) || split_part(my_string,split_part(split_part(my_string,' ',1),'>',2),2) 
WHERE my_id = 1;

以下功能是可选的

您可以制作如下功能

 create or replace function lower_first_word(txt text,del text) returns text as
$$
 select del||lower(split_part(split_part(txt ,' ',1),'>',2)) || split_part(txt ,split_part(split_part(txt ,' ',1),'>',2),2)
$$
language sql 

并更新

UPDATE my_table
    SET my_string = lower_first_word('<root>Here is July''s best food</root>','<root>')
    WHERE my_id = 1;

或者

使用regexsubstring()

select lower(substring('<root>Here is July''s best food</root>',E'[A-Za-z<>]+'))||substring('<root>Here is July''s best food</root>',E'[^ ]* (.*)')

并且更新语句是

UPDATE my_table
SET my_string = lower(substring('<root>Here is July''s best food</root>', E '[A-Za-z<>]+')) || substring('<root>Here is July''s best food</root>', E '[^ ]* (.*)')
WHERE my_id = 1;
于 2015-11-20T09:15:25.903 回答