设为首页 收藏本站
查看: 1280|回复: 0

[经验分享] DB2 字符串截取 SUBSTR

[复制链接]

尚未签到

发表于 2016-11-14 08:43:33 | 显示全部楼层 |阅读模式
  测试数据:
  db2 => select * from temp_table

X
-----------
   11
   22
  223
   10
  120
  113

6 条记录已选择。
  db2 => select * from temp_table where SUBSTR (char(X),9,1)='3'

X
-----------

0 条记录已选择。

db2 => select * from temp_table where SUBSTR (char(X),3,1)='3'

X
-----------
  223
  113

2 条记录已选择。

db2 => select * from temp_table where SUBSTR (char(X),4,1)='3'

X
-----------

0 条记录已选择。

db2 => select * from temp_table where SUBSTR (char(X),8,1)='3'

X
-----------

0 条记录已选择。

db2 => select * from temp_table where SUBSTR (char(X),1,1)='3'

X
-----------

0 条记录已选择。

db2 => select * from temp_table where SUBSTR (char(X),1,1)='1'

X
-----------
   11
   10
  120
  113

4 条记录已选择。

  以下是IBM 官网Doc
  http://publib.boulder.ibm.com/infocenter/dzichelp/v2r2/index.jsp?topic=/com.ibm.db29.doc.sqlref/db2z_bif_substr.htm

SUBSTR

The SUBSTR function returns a substring of a string.




>>-SUBSTR(string-expression,start-+---------+-)----------------><
'-,length-'  



The schema is SYSIBM.


string-expression
An expression that specifies the string from which the result is derived. The string must be a character, graphic, or binary string. If string-expression is a character string, the result of the function is a character string. If it is a graphic string, the result of the function is a graphic string. If it is a binary string, the result of the function is a binary string.
A substring of string-expression is zero or more contiguous characters of string-expression. If string-expression is a graphic string, a character is a DBCS character. If string-expression is a character string or a binary string, a character is a byte. The SUBSTR function accepts mixed data strings. However, because SUBSTR operates on a strict byte-count basis, the result will not necessarily be a properly formed mixed data string.



start
An expression that specifies the position within string-expression to be the first character of the result. The value of the large integer must be between 1 and the length attribute of string-expression. (The length attribute of a varying-length string is its maximum length.) A value of 1 indicates that the first character of the substring is the first character of string-expression.

length
An expression that specifies the length of the resulting substring. If specified, length must be an expression that returns a value that is a built-in large integer data type. The value must be greater than or equal to 0 and less than or equal to n, where n is the length attribute of string-expression - start + 1. The specified length must not, however, be the large integer constant 0.
If length is explicitly specified, string-expression is effectively padded on the right with the necessary number of characters so that the specified substring of string-expression always exists. Hexadecimal zeros are used as the padding character when string-expression is binary data. Otherwise, a blank is used as the padding character.
If string-expression is a fixed-length string, omission of length is an implicit specification of LENGTH( string-expression) - start + 1, which is the number of characters (or bytes) from the character (or byte) specified by start to the last character (or byte) of string-expression. If string-expression is a varying-length string, omission of length is an implicit specification of the greater of zero or LENGTH( string-expression) - start + 1. If the resulting length is zero, the result is an empty string.
If length is explicitly specified by a large integer constant that is 255 or less, and string-expression is not a LOB, the result is a fixed-length string with a length attribute of length. If length is not explicitly specified, but string-expression is a fixed-length string and start is an integer constant, the result is a fixed-length string with a length attribute equal to LENGTH( string-expression) - start + 1. In all other cases, the result is a varying-length string. If length is explicitly specified by a large integer constant, the length attribute of the result is length; otherwise, the length attribute of the result is the same as the length attribute of string-expression.


If any argument of SUBSTR can be null, the result can be null. If any argument is null, the result is the null value. The CCSID of the result is the CCSID of string-expression.
Example 1: FIRSTNME is a VARCHAR(12) column in sample table DSN8910.EMP. When FIRSTNME has the value 'MAUDE':


<!-- -->
   Function:                 Returns:
-----------------------------------
SUBSTR(FIRSTNME,2,3)      -- 'AUD'
SUBSTR(FIRSTNME,2)        -- 'AUDE'
SUBSTR(FIRSTNME,2,6)      -- 'AUDE' followed by two blanks
SUBSTR(FIRSTNME,6)        -- a zero-length string
SUBSTR(FIRSTNME,6,4)      -- four blanks


Example 2: Sample table DSN8910.PROJ contains column PROJNAME, which is defined as VARCHAR(24). Select all rows from that table for which the string in PROJNAME begins with 'W L PROGRAM'.

<!-- -->   SELECT * FROM DSN8910.PROJ
WHERE SUBSTR(PROJNAME,1,12) = 'W L PROGRAM ';


Assume that the table has only the rows that were supplied by DB2®. Then the predicate is true for just one row, for which PROJNAME has the value 'W L PROGRAM DESIGN'. The predicate is not true for the row in which PROJNAME has the value 'W L PROGRAMMING' because, in the predicate's string constant, 'PROGRAM' is followed by a blank.

Example 3: Assume that a LOB locator named my_loc represents a LOB value that has a length of 1 gigabyte. Assign the first 50 bytes of the LOB value to host variable PORTION.

<!-- -->   SET :PORTION = SUBSTR(:my_loc,1,50);


Example 4: Assume that host variable RESUME has a CLOB data type and holds an employee's resume. This example shows some of the statements that find the section of department information in the resume and assign it to host variable DeptBuf. First, the POSSTR function is used to find the beginning and ending location of the department information. Within the resume, the department information starts with the string 'Department Information Section' and ends immediately before the string 'Education Section'. Then, using these beginning and ending positions, the SUBSTR function assigns the information to the host variable.

<!-- -->   SET :DInfoBegPos = POSSTR(:RESUME, 'Department Information Section');
SET :DInfoEnPos = POSSTR(:RESUME, 'Education Section');
SET :DeptBuf = SUBSTR(:RESUME, :DInfoBegPos, :DInfoEnPos - :DInfoBegPos);

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-299951-1-1.html 上篇帖子: DB2中修改表结构 下篇帖子: db2执行计划分析
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表