删除存储过程列中每个条目的前 5 个字符
Removing the first 5 characters of each entry of a column STORED PROCEDURE
我有 table 预订,其中 table 有一个名为 campground 的专栏。
露营地栏的每个条目都与其他条目非常相似,例如:
ALBE/Alberta 海滩家庭房车公园
谁能告诉我如何使用从该列中每个条目中删除 5 个字符的存储过程删除该列中每个条目的前 5 个字符?
到目前为止我试过的代码如下:
UPDATE reservation
SET RIGHT(campground, LEN(campground) - 5)
谁能帮我创建一个具有上述功能的存储过程?
谢谢!!
您可以使用带有起始位置的substring
函数
select substring(campground from 6);
在 MySQL 中,您可以使用非常简单的查询:
UPDATE reservation
SET campground = MID(campground, 6);
在 PostgreSQL 中使用 substring
代替 mid
:
UPDATE reservation
SET campground = substring(campground, 6);
小心,此查询将更改您的源数据,没有回滚选项。
您没有在 set 子句中提供列名:
UPDATE reservation
SET campground = RIGHT(campground, LEN(campground) - 5)
我只想使用 substr()
,这会使表达式更简单:
substr(campground, 6)
MySQL 和 Postgres 都支持这种语法。
如果您想要更新查询,那么:
update reservation set campground = substr(campground, 6)
您真的要删除字符串的前 5 个字符,还是要删除第一个正斜杠之前的所有字符?
此处的其他答案解决了您的字面问题。 这 回答了您可能真的 会问的问题(PostgreSQL 方言):
select substr(campground,position('/' in campground)+1)
from (values
('ABC/American Bike Camp')
,('ALBE/Alberta Beach Family RV Park')) t(campground)
;
substr
------------------------------
American Bike Camp
Alberta Beach Family RV Park
(2 rows)
在更新语句中使用该表达式:
update reservation
set campground = substr(campground,position('/' in campground)+1)
where campground like '%/%'
;
(包含 where 子句以确保它不会更新已经更新的行)
您可以在 PostgreSQL
中使用 split_part()
UPDATE reservation
SET campground = split_part(campground, '/', 2)
我有 table 预订,其中 table 有一个名为 campground 的专栏。 露营地栏的每个条目都与其他条目非常相似,例如:
ALBE/Alberta 海滩家庭房车公园
谁能告诉我如何使用从该列中每个条目中删除 5 个字符的存储过程删除该列中每个条目的前 5 个字符?
到目前为止我试过的代码如下:
UPDATE reservation
SET RIGHT(campground, LEN(campground) - 5)
谁能帮我创建一个具有上述功能的存储过程?
谢谢!!
您可以使用带有起始位置的substring
函数
select substring(campground from 6);
在 MySQL 中,您可以使用非常简单的查询:
UPDATE reservation
SET campground = MID(campground, 6);
在 PostgreSQL 中使用 substring
代替 mid
:
UPDATE reservation
SET campground = substring(campground, 6);
小心,此查询将更改您的源数据,没有回滚选项。
您没有在 set 子句中提供列名:
UPDATE reservation
SET campground = RIGHT(campground, LEN(campground) - 5)
我只想使用 substr()
,这会使表达式更简单:
substr(campground, 6)
MySQL 和 Postgres 都支持这种语法。
如果您想要更新查询,那么:
update reservation set campground = substr(campground, 6)
您真的要删除字符串的前 5 个字符,还是要删除第一个正斜杠之前的所有字符?
此处的其他答案解决了您的字面问题。 这 回答了您可能真的 会问的问题(PostgreSQL 方言):
select substr(campground,position('/' in campground)+1)
from (values
('ABC/American Bike Camp')
,('ALBE/Alberta Beach Family RV Park')) t(campground)
;
substr
------------------------------
American Bike Camp
Alberta Beach Family RV Park
(2 rows)
在更新语句中使用该表达式:
update reservation
set campground = substr(campground,position('/' in campground)+1)
where campground like '%/%'
;
(包含 where 子句以确保它不会更新已经更新的行)
您可以在 PostgreSQL
中使用split_part()
UPDATE reservation
SET campground = split_part(campground, '/', 2)