sql - 从字符串中删除最后两个空格
问题描述
如果我的字符串的长度是 30。我必须从字符串中删除最后 2 个空格。
如果我的字符串的长度是 29。我必须从字符串中删除最后一个空格。
例如。COADC Cathy & Ralph Ward Jr 73
应该是COADC Cathy & Ralph WardJr73
而不是COADCCathy&RalphWardJr73
尝试了, 和trim()
空格instr
但没有成功。还有其他功能吗?substr
replace()
解决方案
我会为此目的创建一个函数:
create function reducestr
(str in varchar2,
len in number,
sp in varchar2 := ' '
) return varchar2 is
pos number;
buf varchar2(4000);
begin
buf := trim(sp from str);
loop
if length(buf) <= len then
-- great, we're already at or under the target length
return buf;
else
-- find the position of the last occurrence of a space
pos := instr(buf, sp, -1);
if pos = 0 then
-- no more spaces to remove, just truncate the string
return substr(buf, 1, len);
else
-- remove the last space
buf := substr(buf, 1, pos-1) || substr(buf, pos+1);
end if;
end if;
end loop;
end reducestr;
select reducestr('COADC Cathy & Ralph Ward Jr 73',28) from dual;
COADC Cathy & Ralph WardJr73
推荐阅读
- facebook - 如何从 facebook 页面聊天框开始获取 url?
- css - GridView 只取 css 文件中的第一个 CSSClass
- python - 读取 CSV,使用 pyproj Transform 更改 2 列并保存到新的 CSV
- ios - Swift noob 问题:尝试在应用程序加载时加载函数
- azure - 如何使用 Azure 数据工厂在 2 个 SQL 服务器之间迁移存储过程?
- sql - 如何检查光标范围内的最大值?
- gmail - 无法从 plesk 网络邮件发送到 Gmail“简单邮件”
- jquery - 如何使用 jquery 更改 CurrentCulture
- python-3.x - .bash_profile 中是否有特定的环境变量存储格式?
- terminal - 使用主机文件同时访问旧/新站点