使用DB2时的一些问题
?
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如下 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 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 null10,DB2 substring涵数是substr(str,strart,count),strart以1开始 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |