我有一个给定的数据库表,在这里我无法更改数据库设计。
此表包含保存的列whare名称。"UserName“栏目包含”克林顿法案“、”特朗普唐纳德“或”布什乔治“等价值观。
我需要一个能够找到“比尔·克林顿”或“唐纳德·特朗普”-row的主钥匙的选择性声明。
因此,Firstname和Lastname的顺序颠倒了。有人知道该怎么做吗。
克劳斯应该是什么样的人。
发布于 2022-09-20 11:11:39
如果您想“反转”这样的字符串(包含两个单词),可以使用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'https://stackoverflow.com/questions/73785660
复制相似问题