使用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里应该怎么写才能实现这个查询啊?

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开始

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值