SUBSTR

关于SUBSTR的官方解释![@more@]

SUBSTR

Syntax

substr::=

Text description of functions84.gif follows
Text description of substr


Purpose

The "substring" functions return a portion of string, beginning at character position, substring_length characters long. 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 codepoints. SUBSTR4 uses UCS4 codepoints.

  • If position is 0, then it is treated as 1.
  • If position is positive, then Oracle counts from the beginning of string to find the first character.
  • If position is negative, then Oracle counts backward from the end of string.
  • If substring_length is omitted, then Oracle returns all characters to the end of string. If substring_length is less than 1, then a null is returned.

string can be any of the datatypes CHAR, VARCHAR2, NCHAR, NVARCHAR2, CLOB, or NCLOB. The return value is the same datatype as string. Floating-point numbers passed as arguments to SUBSTR are automatically converted to integers.

Examples

The following example returns several specified substrings of "ABCDEFG":

SELECT SUBSTR('ABCDEFG',3,4) "Substring"
     FROM DUAL;
 
Substring
---------
CDEF

SELECT SUBSTR('ABCDEFG',-5,4) "Substring"
     FROM DUAL;

Substring
---------
CDEF

Assume a double-byte database character set:

SELECT SUBSTRB('ABCDEFG',5,4.2) "Substring with bytes"
     FROM DUAL;

Substring with bytes
--------------------
CD
加一个例子:
decode用法2
  表、视图结构转化
  现有一个商品销售表sale,表结构为:
  month    char(6)      --月份
  sell    number(10,2)    --月销售金额
  
  现有数据为:
  200001  1000
  200002  1100
  200003  1200
  200004  1300
  200005  1400
  200006  1500
  200007  1600
  200101  1100
  200202  1200
  200301  1300
  
  想要转化为以下结构的数据:
  year   char(4)        --年份
  ------------  ---------------------
  month1  number(10,2)   --1月销售金额
  month2  number(10,2)   --2月销售金额
  month3  number(10,2)   --3月销售金额
  month4  number(10,2)   --4月销售金额
  month5  number(10,2)   --5月销售金额
  month6  number(10,2)   --6月销售金额
  month7  number(10,2)   --7月销售金额
  month8  number(10,2)   --8月销售金额
  month9  number(10,2)   --9月销售金额
  month10  number(10,2)   --10月销售金额
  month11  number(10,2)   --11月销售金额
  month12  number(10,2)   --12月销售金额
  
  结构转化的SQL语句为:
  create or replace view
  v_sale(year,month1,month2,month3,month4,month5,month6,month7,month8,month9,month10,month11,month12)
  as
  select
  substrb(month,1,4),
  sum(decode(substrb(month,5,2),'01',sell,0)),
  sum(decode(substrb(month,5,2),'02',sell,0)),
  sum(decode(substrb(month,5,2),'03',sell,0)),
  sum(decode(substrb(month,5,2),'04',sell,0)),
  sum(decode(substrb(month,5,2),'05',sell,0)),
  sum(decode(substrb(month,5,2),'06',sell,0)),
  sum(decode(substrb(month,5,2),'07',sell,0)),
  sum(decode(substrb(month,5,2),'08',sell,0)),
  sum(decode(substrb(month,5,2),'09',sell,0)),
  sum(decode(substrb(month,5,2),'10',sell,0)),
  sum(decode(substrb(month,5,2),'11',sell,0)),
  sum(decode(substrb(month,5,2),'12',sell,0))
  from sale
  group by substrb(month,1,4);
其中substrb(month,1,4)说是是年份,2000什么的。这里是从具体数据说的,
substrb(month,5,2),可选的就是01,02。。。。12。
请使用浏览器的分享功能分享到微信等