1.“||”的字符连接问题
在DB2中是用“||”连接字符串的,这点与别的语言和数据库是使用“+”有很大区别。在使用“||”时经常会出现“[IBM][CLI Driver][DB2/6000] SQL0440N 未找到类型为 "FUNCTION" 命名为 "||" 且具有兼容自变量的已授权例程。 SQLSTATE=42884”的错误,原因是有非字符类型参与了“||”运算,如有变量或字段出现在“||”的运算中,解决办法是把变量或字段传给char()函数再参与运算就OK了。下面举个例子:
select A.INF || '-' || A.IYF || '-01' from tableA A --会出错,INF年份,IYF月份
select char(A.INF) || '-' || char(A.IYF) || '-01' from tableA A --正确
2.时间比较问题
在DB2中不能使用DATEDIFF函数,或者说是我的不能用,那么怎么比较两个时间相差多少并返回以日、月或年等为时间单位的值呢?用timestampdiff()函数!例子:
select timestampdiff(64,char(timestamp(A.DCRAETETIME)- timestamp('2007-5-25-13.56.41'))) from tableA A
--其中64表示返回以月为单位的值
比如现在是oracle是::sql = sql + " and LoginTime > = to_date('"+ startTime + " 00:00:00" + "','yyyy-mm-dd hh24:mi:ss')";
那在DB2里应该怎么写才能实现这个查询啊?
DB2如下
TO_DATE('2000-01-01 10:00:01','yyyy-mm-dd hh24:mi:ss')
3.like后面不能跟字段
如这样写会出错:
select item1 from table1 where item2 like item3||'%'
如果仅仅是想写像上面那样的like,可以用left或substr函数:
select item1 from table1 where left(item2,length(item3))=item3
或
select item1 from table1 where substr(item2,1,length(item3))=item3
1,isnull convert coalesce function
2,string link SQL char+ convert || char
3,join-condition,It cannot contain any subqueries
4,SQLServer 的 top 或 Oracle rownum函数,在DB2中:
select * from (select ROW_NUMBER() over(ordey org.id desc) as a, org.* from org) as temp where a>=5 and a<=100
或者
select * from org order by org.id desc FETCH FIRST 3 ROWS ONLY
5,len convert length function
6,like后面不能跟字段,如select item1 from table1 where item2 like item3||'%',这样会出错,可以用left或substr函数,如
select item1 from table1 where left(item2,length(item3))=item3
或
select item1 from table1 where substr(item2,1,length(item3))=item3
7,to_date日期比较,DB2是TO_DATE('2000-01-01 00:00:00','yyyy-mm-dd hh24:mi:ss')
8,比较两个时间相差多少并返回以日、月或年等为时间单位的值,用timestampdiff()函数.如select timestampdiff(64,char(timestamp(A.DCRAETETIME)- timestamp('2007-5-25-13.56.41'))) from tableA A
9,HQL语句里以冒号变量名后面是is null的,要转换变量名,如from project where :id is null 转换为DB2的 from project where cast(:id as string) is null
10,DB2 substring涵数是substr(str,strart,count),strart以1开始