---
title: "sql中特殊字符串的截取技巧"
author: "weibaojun"
date: "2013-03-22T15:57:11Z"
tags: ["Oracle", "instr", "substr"]
categories: ["数据库"]
slug: "sql"
draft: false
lastmod: "2013-03-22T15:58:09Z"
id: 1834913
url: "https://weibaojun.iteye.com/blog/1834913"
---

在编写sql时，总会遇到对某个有特殊含义的字段值，截取其中一段需要的值返回，如有这样一个字符串SSOC-XXX-13-001，现在需要返回13这个值，可以用如下oracle 函数实现：

```
select substr('SSOC-DSWB-13-001', instr('SSOC-DSWB-12-001', '-', 1, 2) + 1,2)
  from dual
```

 其中用到了substr函数和instr函数

1.SUBSTR（string,start\_position,\[length]）    求子字符串，返回字符串  
解释：string 元字符串  
       start\_position   开始位置（从0开始）  
       length 可选项，子字符串的个数  
2.INSTR（string,subString,position,ocurrence）查找字符串位置  
解释：string：源字符串  
      subString：要查找的子字符串  
      position：查找的开始位置  
      ocurrence：源字符串中第几次出现的子字符串