`
javathinker
  • 浏览: 232800 次
  • 来自: ...
文章分类
社区版块
存档分类
最新评论

DB2 字符串截取 SUBSTR

阅读更多

测试数据:

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);
分享到:
评论

相关推荐

    db2字符串函数大全

    `SUBSTR` 函数用于从指定位置开始截取字符串的一部分。例如: - `SELECT SUBSTR('12345', 1, 2) FROM SYSIBM.SYSDUMMY1;` 返回 `'12'`。 - `SELECT LEFT('aaaaabbb', 2) FROM SYSIBM.SYSDUMMY1;` 返回 `'aa'`,与 `...

    DB2常用函数(初学者必备)

    - **Substr**: 截取字符串的一部分。 示例: ```sql SELECT CONCAT('Hello', ' World') FROM SYSIBM.SYSDUMMY1; ``` 这将返回"Hello World"。 ### 数学函数 数学函数在统计分析和科学计算中扮演着关键角色。DB2...

    DB2函数的详细总结

    它从位置`para2`开始截取字符串,位置索引从1开始。例如,如果你有一个字符串"Hello World",`SUBSTR("Hello World", 6)`将返回"World"。 - `SUBSTR(para1,para2,para3)`:除了开始位置外,这个版本还指定了返回...

    DB2数据库函数大全

    这只是DB2数据库函数的一部分,实际上DB2还提供了许多其他功能强大的函数,包括日期处理、字符串操作、数学计算、类型转换等,它们在数据库查询和报表生成中起着至关重要的作用。掌握这些函数的使用,能极大地提高...

    db2常用函数整理.doc

    31. INSERT, LEFT, RIGHT, LOCATE:字符串操作函数,插入、截取、查找子串。 32. LONG_VARCHAR 和 LONG_VARGRAPHIC:处理长文本和宽字符数据。 33. LTRIM/RTRIM(SYSFUN 和 SYSIBM 模式):去除字符串左侧或右侧的...

    DB2函数大全

    19. **SUBSTR()**:用于从字符串中截取子串。“SELECT SUBSTR('CDNJFDJFJD', 5); SELECT SUBSTR('CDNJFDJFJD', 5, 2);”将分别返回从第5个字符开始的子串和从第5个字符开始长度为2的子串。 20. **SQRT()**:平方根...

    DB2 函数集

    6. **SUBSTR()**: 字符串截取。`SELECT SUBSTR('CDNJFDJFJD',5) FROM BSEMPMS;`和`SELECT SUBSTR('CDNJFDJFJD',5,2) FROM BSEMPMS;`分别截取从第5个字符开始的所有字符和两个字符。 7. **SQRT()**: 平方根。`SELECT...

    Db2 常用命令Db2 常用命令Db2 常用命令.doc

    - **字符串截取**: `SUBSTR(列名, 开始位置, 长度)` - **功能**: 从指定位置开始截取指定长度的子串。 以上就是Db2常用命令及相关知识点的详细介绍。通过掌握这些命令, 可以有效地管理和维护Db2数据库, 提升工作...

    oracle学习文档 笔记 全面 深刻 详细 通俗易懂 doc word格式 清晰 连接字符串

    varchar2 1~4000字节 可变长度字符串,与CHAR类型相比,使用VARCHAR2可以节省磁盘空间,但查询效率没有char类型高 数值类型 Number(m,n) m(1~38) n(-84~127) 可以存储正数、负数、零、定点数和精度为38位的浮点数...

    db2培训文档,语法、函数、存储过程、实例、聚集、递归等

    - `SUBSTR`: 截取字符串。 - `TRIM`: 去除空格。 2. **数学函数**: - `ABS`: 绝对值。 - `ROUND`: 四舍五入。 - `MOD`: 求模。 3. **日期/时间函数**: - `CURRENT_DATE`: 获取当前日期。 - `CURRENT_TIME`: ...

    DB2部分语句

    这行命令将`log_comm_failed`表中的`ip`字段从字符串转换为整数,并加50。 ### 17. 重置连接 重置当前的数据库连接: ```sql connect reset ``` 此命令将关闭并重新打开当前的数据库连接。 ### 18. 断开连接 ...

    计算机二级VFP复习资料.pdf

    5. 字符串处理: 包含了对字符串的各种操作,如ALLTRIM清除空格、LTRIM和RTRIM分别清除左端和右端的空格、SUBSTR和RIGHT用于字符串的截取等。 6. 数据库操作: 包括创建数据库、打开数据库、关闭数据库、数据库的验证...

    access和sql语法区别详细

    3. **截取字符串**: Access 的 `Mid` 函数对应 SQL Server 的 `SubString`,Oracle 的 `SubStr` 和 DB2 的 `Substring`。可以使用 `GetSubStr` 实现通用性。 4. **大小写转换**: Access 中的 `LCase` 和 `UCase...

    京东 PHP工程师面试题和答案.doc

    - **选项 D**: `substr` 并非用来截取字符串的长度,而是返回指定位置的子字符串。 ##### 7. 以下的哪一个数据库产品是采用了 pureXML 技术的数据服务器: - **选项**: B - **答案解析**: - **选项 A**: SQL ...

    21天学sql(sql21days)

    - CHR/CONCAT/INITCAP/UPPER/LOWER/LPAD/RPAD/LTRIM/RTRIM/REPLACE/SUBSTR/TRANSLATE/INSTR/LENGTH等,这些函数用于字符串的拼接、转换、去空格、截取等操作。 还提到了条件查询的相关语句: - WHERE: 用于过滤...

    达内JAVA软件工程师 培训资料 Oracle数据库部分讲义

    - `SUBSTR`: 截取子串。 - `LENGTH`: 字符串长度。 - `LPAD/RPAD`: 左右填充。 - `REPLACE`: 替换字符。 - `TRIM`: 去除两端空白字符。 - **数字函数**: - `ROUND/TRUNC/MOD`: 四舍五入、截断、取模。 - ...

    oracle数据库的功能

    1. **字符串操作**:包括字符串的拼接、截取等功能。 - `CONCAT`:连接两个或多个字符串值。 - `SUBSTR`:从指定位置开始返回指定长度的子串。 2. **日期操作**:处理日期和时间。 - `SYSDATE`:获取当前系统...

    企业oracle数据分析岗位面试题

    - **字符串操作函数**:如`LENGTH`(计算字符串长度)、`LOWER`/`UPPER`(转换大小写)、`LTRIM`/`RTRIM`(去除前后空格)、`SUBSTR`(截取子字符串)。 - **日期操作函数**:如`TO_DATE`(转换为日期格式)、`ADD...

Global site tag (gtag.js) - Google Analytics