ITPub博客

首页 > 数据库 > 数据库开发技术 > db2日期时间转换为字符串

db2日期时间转换为字符串

原创 数据库开发技术 作者:guyuanli 时间:2012-09-14 14:15:37 0 删除 编辑
db2没有oracle那样方便,不过可以写存储过程来实现:[@more@]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 'yyyyMMddhhmmss'
then yyyy || mm || dd || hh || mi ||ss
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

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/7490392/viewspace-1059426/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2009-03-09

  • 博文量
    167
  • 访问量
    669678