我想更改字符串的顺序,如: 姓氏/姓氏的名字/姓氏或姓氏/姓名的名字/姓氏(考虑/之后的空格)
低于我的要求。但第二部分是错误的:
select SUBSTR ('name/surname' , INSTR ('name/surname','/')+1) ||SUBSTR ('name/surname' , 1,INSTR('name/surname','/')-1)
from dual
答案 0 :(得分:2)
使用正则表达式:
SELECT REGEXP_REPLACE( 'name/surname', '^(.*?)/(.*)$', '\2/\1' ) FROM DUAL;
或修剪所有空格:
SELECT REGEXP_REPLACE( ' name / surname ', '^\s*(.*?)\s*/\s*(.*?)\s*$', '\2/\1' ) FROM DUAL;
或在斜杠之前和之后保留一个空格(并修剪前导/训练空白):
SELECT REGEXP_REPLACE( ' name / surname ', '^\s*(.*?)(\s?/\s?)(.*?)\s*$', '\3\2\1' ) FROM DUAL;
使用字符串函数:
WITH names ( text ) AS (
SELECT 'name/surname' FROM DUAL
)
SELECT SUBSTR( text, INSTR( text, '/' ) + 1 ) || '/' || SUBSTR( text, 1, INSTR( text, '/' ) - 1 )
FROM names;
或修剪空白:
WITH names ( text ) AS (
SELECT ' name / surname ' FROM DUAL
)
SELECT TRIM( SUBSTR( text, INSTR( text, '/' ) + 1 ) ) || '/' || TRIM( SUBSTR( text, 1, INSTR( text, '/' ) - 1 ) )
FROM names;
答案 1 :(得分:0)
选择substr('名字/姓氏',instr('名字/姓氏',' /')+ 1)||' / ' || substr(' firstname / lastname',1,instr(' firstname / lastname',' /') - 1)来自dual;