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.