各个数据库的差异及使用(关于case和time functions)

 1. case
1). SQL server
select region_id,
hehe = CASE
      WHEN region_id <3  THEN 'yes'
WHEN  region_id BETWEEN 3 AND 4 THEN 'ss'
      ELSE 'no'
   END

 from dbo."countries"
attention: between a and b ==== ">=a and <=b"


2). oracle
select job_id,max_salary,
decode(max_salary, 6000, ' aaa', 40000, 'ggg', 'sss') OperationName
from HR.JOBS
====or=======================

select job_id,max_salary,
(case
when max_salary<6000 then ' aaa'
when max_salary between 6000 and 40000 then 'ggg'
else  'sss'
end) as jhh
 from HR.JOBS

3). mysql
select maxlen,
(case
when maxlen<2 then '000'
when maxlen between 2 and 3 then '02144545'
else 'ppp'
end
) as sss
from CHARACTER_SETS

4). db2
select
customer_id,
(case
  when customer_id < 26 then 'llll'
  when customer_id between 26 and 33 then 'ppppp'
else 'kkk'
end
)
 from ORDERS.FORTABLE

5). derby
select category_id,
(case
when category_id < 2 then 'oooo'
when category_id between 3 and 7 then 'oooo'
else 'aaaa'
end) as ppoo
 from APP.CATEGORIES

6). sybase
select id,
(case
when id<2 then 'qqq'
when id between 2 and 3 then 'uuu'
else 'dddd'
end) as iodsioi
 from dbo.spt_limit_types

7). informix
select category_id,
(case
when category_id < 3 then 'uuui'
when category_id between 3 and 5 then 'ioio'
else 'yyy'
end
)
from informix.categories

8).access
不支持case

2.year, month, quarter, week
-- oracle
SELECT TO_NUMBER(TO_CHAR(SYSDATE,'YYYY')) year,
       TO_NUMBER(TO_CHAR(SYSDATE,'Q')) quarter,
       TO_NUMBER(TO_CHAR(SYSDATE,'MM')) month,
       TO_NUMBER(TO_CHAR(SYSDATE,'WW')) week,
       SYSDATE
FROM DUAL;
result:
year   quarter   month   week   SYSDATE
2006   3         8       35     2006-08-28 20:13:27.0


--sybase
/**
select datepart(year, t.starttime) year,
       datepart(quarter, t.starttime) quarter,
       datepart(month, t.starttime) month,
       datepart(week, t.starttime) week,
       t.starttime
from dbo.sysengines t
result:
year   quarter   month   week   SYSDATE
2006   3         8       34     2006-08-26 20:13:27.0


3.1) sql server 2000
select datepart(year, t.crdate) year,
             datepart(quarter, t.crdate) quarter,
             datepart(month, t.crdate) month,
             datepart(week, t.crdate) week,
             t.crdate
from dbo.sysobjects t
result:
year   quarter   month   week   SYSDATE
2000   3         8       33     2000-08-6 20:13:27.0


3.2) sql server 2005
select datepart(year, t.crdate) year,
             datepart(quarter, t.crdate) quarter,
             datepart(month, t.crdate) month,
             datepart(week, t.crdate) week,
             t.crdate
from dbo.sysobjects t
result:
year   quarter   month   week   SYSDATE
2005   4         10       42     2005-10-14 20:13:27.0


4) db2
Select year(current timestamp) yaer,
       quarter(current timestamp) yaer,
       month(current timestamp) yaer,
       week(current timestamp) yaer,
       current timestamp
from sysibm.sysdummy1
result:
year   quarter   month   week   SYSDATE
2006   3         8       35     2006-08-28 20:13:27.0


5) mysql
Select year(now()) yaer,
       quarter(now()) yaer,
       month(now()) yaer,
       week(now()) yaer,
       now()
result:
year   quarter   month   week   SYSDATE
2006   3         8       35     2006-08-28 20:13:27.0


quarter, week not support by derby
6) derby
Select year(current timestamp) yaer,
       (month(current timestamp) + 2) / 3 quarter,
       --quarter(current timestamp) yaer,    quarter not support by derby
       month(current timestamp) yaer,
       --week(current timestamp) yaer,       week not support by derby
       current timestamp
from sysibm.sysdummy1
result:
year   quarter   month   week   SYSDATE
2006   3         8       35     2006-08-28 20:13:27.0


quarter, week not support by informix
7) informix
Select year( current) year,
        round((month( current) - 2) / 3 + 1),
    --quarter( current) month,
               month( current) month,
   --week( current) month
               current
from informix.categories
result:
year   quarter   month   SYSDATE
2006   3         8       2006-08-28 20:13:27.0


quarter, week not support by access
8) access
select orderdate,
       year(orderdate) as 'year',
       int((orderdate + 2) / 3)) as 'quarter',
       Month(orderdate) as 'month'
from orders
PS: the result return the "month" from 1-12.

<script type=text/javascript> </script> <script src="http://pagead2.googlesyndication.com/pagead/show_ads.js" type=text/javascript> </script>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值