Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/86.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在sql中,当字符串以数字结尾时,请删除单词之间的空格_Sql_Oracle - Fatal编程技术网

在sql中,当字符串以数字结尾时,请删除单词之间的空格

在sql中,当字符串以数字结尾时,请删除单词之间的空格,sql,oracle,Sql,Oracle,我的角色长度是30。 如果字符串以数字结尾,我必须删除最后一个空格 例如 COADC Cathy&Ralph Ward Jr 73应该是COADC Cathy&Ralph WardJr73 而不是CoadCathy&RalphWardJr73 尝试regexp\u like获取以数字结尾的字符串,然后尝试trim()和replace()空格,但没有成功。还有其他功能吗 如果字符串以数字结尾,请删除该字符串的最后一个空格 您可以使用regexp\u replace(): [这里][1]是一把小提琴

我的角色长度是30。 如果字符串以数字结尾,我必须删除最后一个空格

例如
COADC Cathy&Ralph Ward Jr 73
应该是
COADC Cathy&Ralph WardJr73

而不是
CoadCathy&RalphWardJr73

尝试
regexp\u like
获取以数字结尾的字符串,然后尝试
trim()
replace()
空格,但没有成功。还有其他功能吗

如果字符串以数字结尾,请删除该字符串的最后一个空格

您可以使用
regexp\u replace()

[这里][1]是一把小提琴

请注意,您的示例删除了最后两个空格。此示例遵循您的描述,而不是您的示例。它还将“以数字结尾”解释为“最后一个空格后面只有数字”

[1] :?>如果字符串以数字结尾,请删除该字符串的最后一个空格。 rdbms=oracle_11.2&FIDLE=995e20520454ee8ffb8030ab156f7431

declare @s varchar(100)
set @s = 'COADC Cathy & Ralph Ward Jr 73'

set @s = case 
            when  isnumeric(right(@s, 1)) = 1 and len(@s) = 30 then reverse(STUFF(reverse(@s), PATINDEX('% %', reverse(@s)), 1, '')) 
            else @s 
        end

set @s = case 
            when  isnumeric(right(@s, 1)) = 1 and len(@s) = 29 then reverse(STUFF(reverse(@s), PATINDEX('% %', reverse(@s)), 1, '')) 
            else @s 
        end

select @s, len(@s)
首次运行此查询时,它将从所有长度为30个字符且以数字结尾的字符串中删除最后一个空格

之后,只需将
len(@s)=30更改为
len(@s)=29,然后再次运行它


这不是一个完美的解决方案,但我看不出有任何理由不起作用。

您的示例删除了两个空格。对不起。我没有补充。如果长度为30,则必须从末端删除2个空格。如果长度为29,则必须删除1space@arsha,考虑到您的评论,我更新了我的答案。如果我只需要从字符串中删除空格(仅考虑长度,而不考虑以数字结尾),是否可以使用regexp_replace()替换最后2个空格。regexp_将如何取代work?@arsha。我想你应该问一个新的问题,有一个更清晰的定义和例子。它不支持stuff@arsha我相信你可以用Gordon答案中的
regexp\u替换部分。重点是运行两次,第一次从所有30个中删除最后一个空格,使其成为29个,第二次从所有29个中删除最后一个空格。
declare @s varchar(100)
set @s = 'COADC Cathy & Ralph Ward Jr 73'

set @s = case 
            when  isnumeric(right(@s, 1)) = 1 and len(@s) = 30 then reverse(STUFF(reverse(@s), PATINDEX('% %', reverse(@s)), 1, '')) 
            else @s 
        end

set @s = case 
            when  isnumeric(right(@s, 1)) = 1 and len(@s) = 29 then reverse(STUFF(reverse(@s), PATINDEX('% %', reverse(@s)), 1, '')) 
            else @s 
        end

select @s, len(@s)