在where子句中将小数转换为date [英] convert decimal to date during where clause

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

问题描述


可能重复:

将DB2 SQL Decimal转换为DATE

我有一个db2数据库,我想在select语句的where子句期间将小数转换为日期。

I have a db2 database and I want to convert a decimal to a date during the where clause in a select statement.

根据月份,十进制日期可以是12或13个字符长。

The decimal date could be 12 or 13 characters long depending on the month.

12个字符

1 ,241,999.00应该成为:1999 / 1 / 24

1,241,999.00 should become: 1999/1/24

13个字符:

12 ,241,999.00应该成为:1999 / em> / 24

列名称为DECIMALCOLUMN:

The column name is DECIMALCOLUMN:

Select * from table1 WHERE
cast(replace(convert,DECIMALCOLUMN,date)) = @date


推荐答案

我看到:你想要一些重新排列数字的数字成为日期的方式。看起来编码是 [m] mddyyyy 。 (为什么不是数据库中的日期数据类型?)

I see: You want some way of rearranging the digits of a number to become a date. It looks like the encoding is [m]mddyyyy. (Why isn't that a date datatype in the database?)

该数字需要转换为字符串,然后将子字符串排列并转换为日期。鉴于这里的复杂性,应该编写一个转换函数来代替被改变为正确数据类型的字段。这样做应该做(未经测试,无法访问db2):

The number needs to be converted to a string and then substrings arranged and converted to a date. Given the complexities here, a conversion function should be written in lieu of the field being altered to be a proper datatype. Something like this should do it (untested, no access to db2):

create function decimaldate(decdate DECIMAL) 
returns DATE
return
with tmp (dstr) as
(
    select substr(digits (decdate),8)
    from sysibm.sysdummy1
)
select
  date (substr(dstr,4,4) || '-' ||
        substr(dstr,1,2) || '-' ||
        substr(dstr,3,2)
       )
from tmp

将字符串重新格式化为 yyyy-mm-dd 并将其转换为日期。 (我使用这个作为基础。)

This converts the number to a string, reformats the string as yyyy-mm-dd and converts to a date. (I used this as a basis.)

因此,原始问题的解决方案只是写:

So the solution to the original question is simply to write:

SELECT * 
FROM table1
WHERE decimaldate(DECIMALCOLUMN) = @date

在数据库中编码日期的一种灵活方式,具有始终可用的功能应该证明是无价的。

With such a screwy way of encoding the date in the database, having the function always available should prove invaluable.

这篇关于在where子句中将小数转换为date的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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