我有一个给定的数据库表,无法更改数据库设计。
此表包含一个保存名称的列。列";用户名";包含类似于";克林顿法案;或";特朗普;或";布什";。
我需要一个select语句,它能够找到"的主键;比尔·克林顿;或";唐纳德·特朗普-一行
所以名字和姓氏的顺序颠倒了。有人知道怎么做吗。
Where Clouse应该是什么样子。
如果您想"反向";这样一个字符串(包含两个单词(,您可以使用substr + instr
或正则表达式,例如
SQL> with test (username) as
2 (select 'Clinton Bill' from dual)
3 select substr(username, instr(username, ' ') + 1) ||' '||
4 substr(username, 1, instr(username, ' ') - 1) as reversed_value,
5 --
6 regexp_substr(username, 'w+', 1, 2) ||' '||
7 regexp_substr(username, 'w+', 1, 1) as reversed_value_2
8 from test;
REVERSED_VALUE REVERSED_VALUE_2
-------------------- --------------------
Bill Clinton Bill Clinton
SQL>
现在,只需在另一个查询中重复使用它(无论您想要哪个选项(,例如
select id
from test
where regexp_substr(username, 'w+', 1, 2) ||' '||
regexp_substr(username, 'w+', 1, 1) = 'Bill Clinton'