我name字段有这样的数据
张三(学)
李四(学)
王五
张额(员)
这里输入法的不同,后面带的括号也不一样,但只有(),()这种情况,没有[]{}这样的情况,我现在只想取前面的名字,后面括号不要,
求一函数
张三(学)
李四(学)
王五
张额(员)
这里输入法的不同,后面带的括号也不一样,但只有(),()这种情况,没有[]{}这样的情况,我现在只想取前面的名字,后面括号不要,
求一函数
解决方案 »
- 请教:如何跟踪delphi开发的程序的数据库操作
- 与大家讨论一个sql的对错。
- 建分区表时,为什么不同的分区要指定不同的多个表空间?都用一个表空间可以么?
- 求SQL
- Oracle 数据库数据丢失,急
- 求一条分类汇总的语句
- 请问为分区表创建索引的时候该如何分区?
- 并非挑战: 怎么样才能让自己的程序像IMP.EXE那样快速地往ORACLE中插入记录
- 怎样查看和理解ORACLE中的约束条件
- 有用ORACLE 8I做过数据仓库的高手吗?我愿意出1000元人民币拜师!如果你只要分数,我愿意给2000分。
- oracle触发器里的enable/disable和valid/invalid状态
- oracle触发器动态表问题
写函数也只是用REF CURSOR返回一条语句的结果集。
---
张三
with test as(
select '张三' name from dual union all
select '张三(XX)' name from dual union all
select '张三(YY)' name from dual)
SELECT CASE
WHEN instr(NAME, '(') > 0 THEN
substr(NAME, 1, instr(NAME, '(') - 1)
WHEN instr(NAME, '(') > 0 THEN
substr(NAME, 1, instr(NAME, '(') - 1)
ELSE
NAME
END
FROM test;
--方法2:使用decode函数
with test as(
select '张三' name from dual union all
select '张三(XX)' name from dual union all
select '张三(YY)' name from dual)
SELECT decode(instr(NAME, '('),
0,
decode(instr(NAME, '('), 0, NAME, substr(NAME, 1, instr(NAME, '(') - 1)),
substr(NAME, 1, instr(NAME, '(') - 1))
FROM test;
SQL> with test as(
2 select '张三' name from dual union all
3 select '张三(XX)' name from dual union all
4 select '张三(YY)' name from dual)
5 select REGEXP_REPLACE(name,'(.+)[\(|\(](.+)','\1') from test;REGEXP_REPLACE(NAME,'(.+)[\(|\
--------------------------------------------------------------------------------
张三
张三
张三SQL>
begin
if instr(name, '(') > 0 then
return substr(name, 0, instr(name, '(') - 1);
elsif instr(name, '(') > 0 then
return substr(name, 0, instr(name, '(') - 1);
else
return name;
end if;
end;
select regexp_replace(a,'(\([[:alnum:]]*\))|(([[:alnum:]]*))') from tt;
decode(instr(replace(name,'(','('),'('),0,length(name),instr(replace(name,'(','('),'(')-1)) from a;
--把最后的a换成你的表名即可
with test as(
select '张三' name from dual union all
select '张三(XX)' name from dual union all
select '张三(YY)' name from dual)
select REGEXP_REPLACE(name,'(.+)\((.+)|(.+)((.+)','\1\3') from test;
select
substr(name,
1,
case when instr(to_single_byte(name),'(') = 0 then length(name)
else instr(to_single_byte(name),'(')-1
end) as name from 表名;--测试
SQL> with tt as(
2 select '张三(学)' name from dual union all
3 select '李四(学)' name from dual union all
4 select '王五' name from dual union all
5 select '张额(员)' name from dual
6 )
7 select
8 substr(name,
9 1,
10 case when instr(to_single_byte(name),'(') = 0 then length(name)
11 else instr(to_single_byte(name),'(')-1
12 end) as name from tt;NAME
--------------------
张三
李四
王五
张额
case when instr(to_single_byte(name),'(') = 0 then name --没有括号取原值
else substr(name,1,instr(to_single_byte(name),'(')-1) --有括号截取到前一位
end as name
from 表名;