您的位置:首页 > 数据库 > Oracle

Oracle开发学习记录

2017-10-13 10:00 323 查看
1、从字符串中获取数字部分内容:

可以通过REGEXP_SUBSTR函数(配合正则表达式)来实现。

举例:

sql:  select regexp_substr('CYJ8-ABC','[0-9]+') from dual;


执行结果:8

sql:select
regexp_substr('华南大润发:920149afc','大润发:([0-9]+)')  subnum
from dual;


执行结果:大润发:920149

说明:直接匹配字符时不需要加双引号正则表达式”[0-9]+”也可以不加括号

备注:

REGEXP_SUBSTR函数格式如下:

function REGEXP_SUBSTR(String, pattern, position, occurrence, modifier)

__srcstr :需要进行正则处理的字符串

__pattern :进行匹配的正则表达式

__position :起始位置,从第几个字符开始正则表达式匹配(默认为1)

__occurrence :标识第几个匹配组,默认为1

__modifier :模式(’i’不区分大小写进行检索;’c’区分大小写进行检索。默认为’c’。)

2、start with … connect by 递归树查询

select ct.code chain_code,ct.parent_code
,connect_by_root code as terminal_code,level
,connect_by_isleaf,ltrim(SYS_CONNECT_BY_PATH(ct.code,'-->'),'-->') code_path
from mama100_owner.crm_terminal ct
start with ct.code = '22053'
connect by  ct.code = prior ct.parent_code
;


执行结果:

22053 920771 22053 1 0 22053

920771 920046 22053 2 0 22053–>920771

920046 22053 3 1 22053–>920771–>920046

说明:

(1)start with : 以编码22053开始寻找其对应的最上层编码;

(2)prior: 运算符PRIOR的位置决定查询时的检索顺序;prior表示上一条记录,这里的上一条记录parent_code为本条记录的code。

备注:

(1)level :对应树的第几层,这里以22053为根节点开始检索,根节点即为第一层,以此类推;

(2)connect_by_root :显示根目录;

(3)connect_by_isleaf :显示当前记录是否为子叶节点,1表示是0为否;

(4)SYS_CONNECT_BY_PATH:把一个父节点下的所有子节点通过某个字符(有些文档说不能用逗号否则数据库会报错,自己测试并没报错不懂咋回事)进行区分,然后连接在一个列中显示。

3、merge into SQL语句示例及注意事项

merge into tablename_a  o
using( select om.ord_id,om.value_after_change,dm.short_name
from tablename_b  om
left join tablename_c on om.value_after_change = dm.office_code
where om.change_column = 'department_code'
and om.change_flag = 0
) o1
on (o.ord_id = o1.ord_id)
when matched then
update set o.department_code = o1.value_after_change
,o.department_name = o1.short_name

;
--update set 后面的语句写成以下写法会出错:
(o.department_code,o.department_name) = (o1.value_after_change,o1.short_name)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: