在DB2中将字符串转换为日期 [英] Converting a string to a date in DB2

查看:2497
本文介绍了在DB2中将字符串转换为日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我第一次使用DB2数据库。



我试图使用DB2日期,但是数据作为字符串存储在DB2数据库。



我想将此日期字符串转换为实际日期,最好是放弃时间,因为我想要2011年1月1日至2011年1月26日之间的所有交易。 p>

基本上,我想在DB2中使用这个MS SQL语句...

  CONVERT(datetime,SETTLEMENTDATE.VALUE,103)

  CAST(SETTLEMENTDATE.VALUE,DATE)

AND

  DATE(SETTLEMENTDATE.VALUE)



但我需要一个DB2 whizzkid的专业知识!



p>

解决方案

根据你自己的答案,我猜你的列的数据格式如下:

 'DD / MM / YYYY HH:MI:SS'

日/月/年之间的实际分隔符无关紧要,也不会是一年之后的分隔符。



你使用的是什么版本的DB2或者它运行的平台,所以我想假设它在Linux,UNIX或Windows上。



几乎任何最新版本DB2 for Linux / UNIX / Windows(8.2或更高版本,甚至更高版本),您可以使用 TRANSLATE 函数:

  select 
date(translate('GHIJ-DE-AB',column_with_date,'ABCDEFGHIJ'))
from
yourtable

对于这个解决方案,在列中的日期之后是什么都没关系。



在DB2 9.7中,您还可以使用 TO_DATE 函数(类似于Oracle的TO_DATE):

 日期(to_date(column_with_date,'DD-MM-YYYY HH:MI:SS'))

这需要您的数据匹配格式化字符串;它看起来更容易理解,但不如TRANSLATE选项那么灵活。


I am working with a DB2 database for the first time.

I am trying to work with DB2 dates, but the data is stored as a string in the DB2 database.

I want to convert this date-string into an actual date, preferably dropping off time because I want all transactions between 1 Jan 2011 and 26 Jan 2011.

So essentially, I want this MS SQL statement in DB2 magic...

CONVERT(datetime,SETTLEMENTDATE.VALUE,103)

For background, I have got as far as

CAST(SETTLEMENTDATE.VALUE, DATE)

AND

DATE(SETTLEMENTDATE.VALUE)

But I need the expert knowledge of a DB2 whizzkid!

Thanks

解决方案

Based on your own answer, I'm guessing that your column has data formatted like this:

'DD/MM/YYYY HH:MI:SS'

The actual separators between Day/Month/Year don't matter, nor does anything that comes after the year.

You don't say what version of DB2 you are using or what platform it's running on, so I'm going to assume that it's on Linux, UNIX or Windows.

Almost any recent version of DB2 for Linux/UNIX/Windows (8.2 or later, possibly even older versions), you can do this using the TRANSLATE function:

select 
   date(translate('GHIJ-DE-AB',column_with_date,'ABCDEFGHIJ'))
from
   yourtable

With this solution it doesn't matter what comes after the date in your column.

In DB2 9.7, you can also use the TO_DATE function (similar to Oracle's TO_DATE):

date(to_date(column_with_date,'DD-MM-YYYY HH:MI:SS'))

This requires your data match the formatting string; it's easier to understand when looking at it, but not as flexible as the TRANSLATE option.

这篇关于在DB2中将字符串转换为日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆