博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Oracle 字符串查询以及拆分函数
阅读量:7243 次
发布时间:2019-06-29

本文共 3277 字,大约阅读时间需要 10 分钟。

hot3.png

select id,SUBSTR(detail,INSTR(detail,'hijk')+5,Instr(detail,';abcd=')-INSTR(detail,'def')-5)from(select a.id,a.detail,a.filefrom table_awhere not exists(select/*+index(b id)*/ id  ##带上所有查询,注意如果用别名,那么表名也用别名fromtable_b bwhere b.id=a.id))where file like '%xxxxxx%'

其中有两个函数需要注意一下:

第一是 INSTR

可以有四个参数

第一是数据源,第二是要查询的字符串,第三是起始位置,第四是出现次数

INSTR( source_string, substring [, start_position [, occurrance ] ] )
INSTR('Welcome to PSOUG.org', 'o')        would return 5   (Finds the first occurrence of 'o')INSTR('Welcome to PSOUG.org', 'o', 1, 1)  would return 5.  (Finds the first occurrence of 'o')INSTR('Welcome to PSOUG.org', 'o', 1, 2)  would return 10. (Finds the second occurrence of 'o')INSTR('Welcome to PSOUG.org', 'o', 1, 3)  would return 18. (Finds the third occurrence of 'o')INSTR('Welcome to PSOUG.org', 'o', -2, 2) would return 10. (Count back 2, then find the 2nd 'o')

INSTR 可以控制查询要搜索的字符串,并且返回出现的位置。

要注意的地方

  1. Both source_string and substring can be any of the datatypes , , , , , or . The value returned is of datatype.
     
  2. Both start_position and occurrence must be an integer of datatype , or any datatype that can be implicitly converted to .
     
  3. The first position in the source_string is 1, not 0. 没有要查询的字符串的时候,返回0
  4. The INSTR search is case sensitive.

http://psoug.org/definition/INSTR.htm

第二是SUBSTR 

有三个参数,第一是数据源,第二是起始位置,第三是长度

参考博文:

SUBSTR functions

The SUBSTR functions (SUBSTR, SUBSTRB, SUBSTRC, SUBSTR2, and SUBSTR4) return a portion of string, beginning at a specified position in the string. The functions vary in how they calculate the length of the substring to return.

  • SUBSTR calculates lengths using characters as defined by the input character set.

  • SUBSTRB calculates lengths using bytes.

  • SUBSTRC calculates lengths using Unicode complete characters.

  • SUBSTR2 calculates lengths using UCS2 code points.

  • SUBSTR4 calculates lengths using UCS4 code points.

Return Value

The return value is the same data type as string.

Syntax

{SUBSTR | SUBSTRB | SUBSTRC | SUBSTR2 | SUBSTR4}(char, position [, substring_length ])

Arguments

string

A text expression that is the base string from which the substring is created.

position

The position at which the first character of the returned string begins.

  • When position is 0 (zero), then it is treated as 1.

  • When position is positive, then the function counts from the beginning of string to find the first character.

  • When position is negative, then the function counts backward from the end of string.

substring_length

The length of the returned string. SUBSTR calculates lengths using characters as defined by the input character set. SUBSTRB uses bytes instead of characters. SUBSTRC uses Unicode complete characters. SUBSTR2 uses UCS2 code points. SUBSTR4 uses UCS4 code points.

When you do not specify a value for this argument, then the function returns all characters to the end of string. When you specify a value that is less than 1, the function returns NA.

Examples

Example 8-120 Retrieving a Charachter Substring

The following example returns several specified substrings of "abcdefg".

SHOW SUBSTR('abcdefg',3,4) cdefSHOW SUBSTR('abcdefg',-5,4) cdef

Example 8-121 Retrieving a Substring Using Bytes

Assume a double-byte database character set.

SHOW SUBSTRB('abcdefg',5,4.2) cd

转载于:https://my.oschina.net/u/2308739/blog/779218

你可能感兴趣的文章
CSS样式设置小技巧
查看>>
PDF 补丁丁 0.4.2.1063 测试版发布:新增检查新版本功能
查看>>
Module的加载实现
查看>>
统计学 学习第一季第一集《统计学习方法》第一章 统计学方法概论 简要概述...
查看>>
css学习_css补充知识
查看>>
python 08day--软件包的管理及ssh、samba、apache服务
查看>>
ThinkPPHP学习(一)生成图片验证码
查看>>
KFC ajax
查看>>
学长哈哈的店公告
查看>>
python 读取 xlsx
查看>>
ethereum/EIPs-1271 smart contract
查看>>
Football
查看>>
java中文件上传下载将file转为MultipartFile
查看>>
冲刺第三天
查看>>
测试oracle数据库连接
查看>>
PhoneGap打Android包报错
查看>>
nginx添加ssl证书
查看>>
Hadoop问题汇总
查看>>
P1042 乒乓球
查看>>
CODEVS 1047 邮票面值设计
查看>>