Chinaunix首页 | 论坛 | 博客
  • 博客访问: 115558
  • 博文数量: 24
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 0
  • 用 户 组: 普通用户
  • 注册时间: 2016-11-22 14:58
个人简介

坚持,做最好的自己

文章分类

全部博文(24)

文章存档

2015年(2)

2014年(9)

2013年(13)

我的朋友

分类: DB2/Informix

2015-11-19 15:02:14

数据库中有三种时间类型字段,分别是date,time,timestamp
日期转字符串比较麻烦
VARCHAR_FORMAT等同于to_char
日期型转换成字符串型时,第一个参数不能是date型,必须是timestamp型。
可以使用timestamp_iso函数先进行转换一次

 

I want to convert a Date column to a formatted string in DB2. This SQL works fine:

select varchar_format(current timestamp, 'YYYY-MM')
from sysibm.sysdummy1; 

but this SQL gives an error:

select varchar_format(current date, 'YYYY-MM')
from sysibm.sysdummy1; 

The error is: [SQL0171] Argument 1 of function VARCHAR_FORMAT not valid.

In the first SQL, the first arg for VARCHAR_FORMAT is a timestamp, and that works. In the second SQL, the first arg for VARCHAR_FORMAT is a date, and that doesn't work.

The IBM doc implies that there's only this one function, VARCHAR_FORMAT (and its synonym, TO_CHAR).

How am I supposed to convert a DATE (not a TIMESTAMP) to a string? Or, do I have to convert the DATE to a TIMESTAMP first, then use VARCHAR_FORMAT?

I am running DB2 7.1 for i Series.

Update: converting to TIMESTAMP_ISO works. But it's ugly:

select varchar_format(timestamp_iso(current date), 'YYYY-MM')
from sysibm.sysdummy1; 

That one works.





阅读(4669) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~