当前位置:首页 > DB2存储过程学习总结
附:java中substring(index1,index2)的简单用法
作用:从字符串索引(下标)为index1的字符开始截取长度为index2-index1 的字符串。 String str=\
System.out.println(str.substring(0,5)); 打印结果为:Hello
? LOCATE(substr,str), LOCATE(substr,str,pos) 第一个语法返回字符串str第一次出现的子串substr的位置。第二个语法返回第一次出现在字符串str的子串substr的位置,从位置pos开始。 substr不在str中,则返回0。 SQL> SELECT LOCATE('bar', 'foobarbar'); +---------------------------------------------------------+ | LOCATE('bar', 'foobarbar') | +---------------------------------------------------------+ | 4 | +---------------------------------------------------------+ 1 row in set (0.00 sec) ? ?
? REPLACE函数的使用
REPLACE
用第三个表达式替换第一个字符串表达式中出现的所有第二个给定字符串表达式。 语法
REPLACE ( ''string_replace1'' , ''string_replace2'' , ''string_replace3'' ) 参数
''string_replace1''
待搜索的字符串表达式。string_replace1 可以是字符数据或二进制数据。 ''string_replace2''
待查找的字符串表达式。string_replace2 可以是字符数据或二进制数据。
''string_replace3''
替换用的字符串表达式。string_replace3 可以是字符数据或二进制数据。 返回类型
如果 string_replace(1、2 或 3)是支持的字符数据类型之一,则返回字符数据。如果 string_replace(1、2 或 3)是支持的 binary 数据类型之一,则返回二进制数据。 示例
下例用 xxx 替换 abcdefghi 中的字符串 cde。 SELECT REPLACE(''abcdefghicde'',''cde'',''xxx'')GO 下面是结果集:
------------abxxxfghixxx(1 row(s) affected)
? SQL的partition by 字段(可实现自动分配组号跟归组合并)
先看例子:
if object_id('TESTDB') is not null drop table TESTDB create table TESTDB(A varchar(8), B varchar(8)) insert into TESTDB select 'A1', 'B1' union all select 'A1', 'B2' union all select 'A1', 'B3' union all select 'A2', 'B4' union all select 'A2', 'B5' union all select 'A2', 'B6' union all select 'A3', 'B7' union all select 'A3', 'B3' union all select 'A3', 'B4' -- 所有的信息
SELECT * FROM TESTDB A B
------- A1 B1 A1 B2 A1 B3 A2 B4 A2 B5 A2 B6 A3 B7 A3 B3 A3 B4
-- 使用PARTITION BY 函数后
SELECT *,ROW_NUMBER() OVER(PARTITION BY A ORDER BY A DESC) NUM FROM TESTDB A B NUM ------------- A1 B1 1 A1 B2 2 A1 B3 3 A2 B4 1 A2 B5 2 A2 B6 3 A3 B7 1 A3 B3 2 A3 B4 3
可以看到结果中多出一列NUM 这个NUM就是说明了相同行的个数,比如A1有3个,他就给每个A1标上是第几个。
-- 仅仅使用ROW_NUMBER() OVER的结果
SELECT *,ROW_NUMBER() OVER(ORDER BY A DESC)NUM FROM TESTDB
A B NUM ------------------------
A3 B7 1 A3 B3 2 A3 B4 3 A2 B4 4 A2 B5 5 A2 B6 6 A1 B1 7 A1 B2 8 A1 B3 9
可以看到它只是单纯标出了行号。
?
db2 循环示例
--首先是 for 循环 begin atomic declare v_id int;
declare v_name varchar(30); for v_c as select id,name from J1 do
set v_name=v_c.id||v_c.name;
insert into j1 values(v_c.id+10,v_name); end for; end
--然后是while 循环
共分享92篇相关文档