Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/80.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_Plsql_Regexp Replace - Fatal编程技术网

Sql 删除字符串中的一个单词(或两个空格之间的单词)

Sql 删除字符串中的一个单词(或两个空格之间的单词),sql,oracle,plsql,regexp-replace,Sql,Oracle,Plsql,Regexp Replace,我有这个: Dr. LeBron Jordan John Bon Jovi 我想这样: Dr. Jordan John Jovi 我该怎么做?我想这是regexp\u替换 谢谢你的关注。 非常感谢您的帮助。如果只是两个字,它会回复您的。(“勒布朗·乔丹”将回归“勒布朗·乔丹”) 如果是三个字,它将取出中间词(LeBron Jordan博士)将返回“Jordan博士” 如果只有两个词,它会返回该词。(“勒布朗·乔丹”将回归“勒布朗·乔丹”) 如果是三个字,它将取出中间词(LeBron Jord

我有这个:

Dr. LeBron Jordan
John Bon Jovi
我想这样:

Dr. Jordan
John Jovi
我该怎么做?我想这是regexp\u替换

谢谢你的关注。
非常感谢您的帮助。

如果只是两个字,它会回复您的。(“勒布朗·乔丹”将回归“勒布朗·乔丹”)

如果是三个字,它将取出中间词(LeBron Jordan博士)将返回“Jordan博士”


如果只有两个词,它会返回该词。(“勒布朗·乔丹”将回归“勒布朗·乔丹”)

如果是三个字,它将取出中间词(LeBron Jordan博士)将返回“Jordan博士”


在SQL Server中尝试下面的单个语句:

declare@fullname varchar(200)
选择@fullname='John Bon Jovi'

选择子字符串(@fullname,1,charindex(“”,@fullname,1))+子字符串(@fullname,charindex(“”,@fullname,charindex(“”,@fullname,1)+1,len(@fullname)-charindex(“”,@fullname,charindex(“”,@fullname,1))
在SQL Server中尝试下面的单个语句:

declare@fullname varchar(200)
选择@fullname='John Bon Jovi'

选择子字符串(@fullname,1,charindex(“”,@fullname,1))+子字符串(@fullname,charindex(“”,@fullname,charindex(“”,@fullname,1)+1,len(@fullname)-charindex(“”,@fullname,charindex(“”,@fullname,1))
这里有一种使用regexp\u replace的方法,如您所述,使用多种形式的名称进行测试。比嵌套的SUBSTR()和INSTR()更强大,但您需要了解正则表达式,这将使您在学习后能够为更复杂的模式提供更强大的模式匹配能力:

with tbl as (
  select 'Dr. LeBron Jordan' data from dual
  union
  select 'John Bon Jovi' data from dual
  union
  select 'Yogi Bear' data from dual
  union
  select 'Madonna' data from dual 
  union
  select 'Mr. Henry Cabot Henhouse' data from dual  ) 

select regexp_replace(data, '^([^ ]*) .* ([^ ]*)$', '\1 \2') corrected_string from tbl;

CORRECTED_STRING
----------------
Dr. Jordan
John Jovi
Madonna
Mr. Henhouse
Yogi Bear
正则表达式可以理解为:

^      At the start of the string (anchor the pattern to the start)
(      Start remembered group 1
[^ ]*  Zero or more characters that are not a space
)      End remembered group 1
space  Where followed by a literal space
.      Followed by any character
*      Followed by any number of the previous any character
space  Followed by another literal space
(      Start remembered group 2
[^ ]*  Zero or more characters that are not a space
)      End remembered group 2
$      Where it occurs at the end of the line (anchored to the end)
那么“\1\2”表示返回记忆组1,后跟空格,后跟记忆组2

如果找不到模式,则返回原始字符串。这可以通过用方括号包围返回的组并再次运行来看到:

...
select regexp_replace(data, '^([^ ]*) .* ([^ ]*)$', '[\1] [\2]')
corrected_string from tbl;

CORRECTED_STRING
[Dr.] [Jordan]
[John] [Jovi]
Madonna
[Mr.] [Henhouse]
Yogi Bear

正如您所提到的,这里有一种使用regexp_replace的方法,使用几种形式的名称进行测试。比嵌套的SUBSTR()和INSTR()更强大,但您需要了解正则表达式,这将使您在学习后能够为更复杂的模式提供更强大的模式匹配能力:

with tbl as (
  select 'Dr. LeBron Jordan' data from dual
  union
  select 'John Bon Jovi' data from dual
  union
  select 'Yogi Bear' data from dual
  union
  select 'Madonna' data from dual 
  union
  select 'Mr. Henry Cabot Henhouse' data from dual  ) 

select regexp_replace(data, '^([^ ]*) .* ([^ ]*)$', '\1 \2') corrected_string from tbl;

CORRECTED_STRING
----------------
Dr. Jordan
John Jovi
Madonna
Mr. Henhouse
Yogi Bear
正则表达式可以理解为:

^      At the start of the string (anchor the pattern to the start)
(      Start remembered group 1
[^ ]*  Zero or more characters that are not a space
)      End remembered group 1
space  Where followed by a literal space
.      Followed by any character
*      Followed by any number of the previous any character
space  Followed by another literal space
(      Start remembered group 2
[^ ]*  Zero or more characters that are not a space
)      End remembered group 2
$      Where it occurs at the end of the line (anchored to the end)
那么“\1\2”表示返回记忆组1,后跟空格,后跟记忆组2

如果找不到模式,则返回原始字符串。这可以通过用方括号包围返回的组并再次运行来看到:

...
select regexp_replace(data, '^([^ ]*) .* ([^ ]*)$', '[\1] [\2]')
corrected_string from tbl;

CORRECTED_STRING
[Dr.] [Jordan]
[John] [Jovi]
Madonna
[Mr.] [Henhouse]
Yogi Bear

这是哪个数据库?Gary_W和Hemant Patel都值得称赞。再次感谢。这是哪个数据库?Gary_W和Hemant Patel都值得表扬。再次感谢。我收到一个ORA-00904错误:“子字符串”:无效标识符,感谢您的尝试请尝试oracle的编辑版本并参考Fidle链接以获取相同的示例:我收到一个ORA-00904错误:“子字符串”:无效标识符,感谢您的尝试请尝试oracle的编辑版本,并参考Fidle链接了解相同的示例:您使用的是什么DBMS?我是针对SQL Server 2012Oracle SQL执行此操作的。(我使用的是SQL Developer。)也谢谢您的尝试。您使用的是什么数据库管理系统?我是针对SQL Server 2012Oracle SQL执行此操作的。(我正在使用SQLDeveloper。)也谢谢你的尝试。Gary,谢谢你的正则表达式过滤器。我已经使用它们一段时间了,但不知道您无法对所有元素进行分组,例如,您没有对可选中间名的.*占位符进行分组。我也学到了一些新的东西。很高兴它有帮助。诀窍是将字符串模式描述为正则表达式,并围绕要使用的部分进行分组。Gary,感谢正则表达式过滤器。我已经使用它们一段时间了,但不知道您无法对所有元素进行分组,例如,您没有对可选中间名的.*占位符进行分组。我也学到了一些新的东西。很高兴它有帮助。诀窍是将字符串模式描述为正则表达式,并围绕要使用的部分分组。