您的位置:首页 > 数据库

DB2 常用日期SQL语句

2011-11-27 14:38 288 查看
--获取上个月末日期格式为yyyyMMdd

values current timestamp -day(current date) days

values ts_fmt(current timestamp -day(current date) days,'yyyymmdd')

--获取当前月份

values month(current date)

--获取本月的第一天

values current timestamp -day(current date) days+ 1 days

--获取本月的最后一天

values current timestamp+1 months -day(current date) days

--获取上月的第一天

values current timestamp -1 months-day(current date) days+ 1 days

--获取上月的最后一天

values current timestamp -day(current date) days



--创建一个日期格式转换
create function ts_fmt(TS timestamp, fmt varchar(20))
returns varchar(50)
return
with tmp (dd,mm,yyyy,hh,mi,ss,nnnnnn) as
(
select
substr( digits (day(TS)),9),
substr( digits (month(TS)),9) ,
rtrim(char(year(TS))) ,
substr( digits (hour(TS)),9),
substr( digits (minute(TS)),9),
substr( digits (second(TS)),9),
rtrim(char(microsecond(TS)))
from sysibm.sysdummy1
)
select
case fmt
when 'yyyymmdd'
then yyyy || mm || dd
when 'mm/dd/yyyy'
then mm || '/' || dd || '/' || yyyy
when 'yyyy/dd/mm hh:mi:ss'
then yyyy || '/' || mm || '/' || dd || ' ' || 
hh || ':' || mi || ':' || ss
when 'nnnnnn'
then nnnnnn
else
'date format ' || coalesce(fmt,' ') || 
' not recognized.'
end
from tmp








具体关于DB2日期文章 参考:http://www.ibm.com/developerworks/cn/data/library/techarticles/0211yip/0211yip3.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: