测试数据:
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
SUBSTR
The SUBSTR function returns a substring of a string.
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.
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
<!-- --> 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.
<!-- --> SET :PORTION = SUBSTR(:my_loc,1,50);
<!-- --> SET :DInfoBegPos = POSSTR(:RESUME, 'Department Information Section'); SET :DInfoEnPos = POSSTR(:RESUME, 'Education Section'); SET :DeptBuf = SUBSTR(:RESUME, :DInfoBegPos, :DInfoEnPos - :DInfoBegPos);