转换int YYYYMMDD为日期AS400

时间:2019-04-26 07:41:29

标签: sql excel db2 ibm-midrange

我需要在AS400中将整数转换为日期格式。

我有一个名为<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <table id="example"> <thead> <tr> <th>ID</th> <th>strSO</th> <th>PackName</th> <th>DocType</th> <th>DocName</th> <th>Actions</th> <th>Preview</th> </tr> </thead> <tbody></tbody> </table>的字段,它是格式为ivdat8的整数。

我需要使用WHERE子句来选择今天和3天前两个日期之间的数据。

我正在使用以下代码来做到这一点:

YYYYMMDD

sysibm中的当前日期是真实的日期格式,而ivdat是整数。

如何将ivdat8转换为可在WHERE子句中使用的日期格式?

我尝试了以下方法将int转换为日期:

Where
    ivdat8 Between (Select current date - 3 days from sysibm.sysdummy1) And (Select current date from sysibm.sysdummy1)

cast(cast(A.ivdat8 as varchar(50)) as date)

4 个答案:

答案 0 :(得分:3)

实际上,最好不要转换ivdat8列数据,而是使用下面的参数进行转换。

select ivdat8
from table (values 
  int(to_char(current date - 2 days, 'YYYYMMDD'))
, int(to_char(current date - 5 days, 'YYYYMMDD'))
) t (ivdat8)
where ivdat8 between 
    int(to_char(current date - 3 days, 'YYYYMMDD')) 
and int(to_char(current date, 'YYYYMMDD'));

答案 1 :(得分:1)

最简单的方法是在不引起查询复杂转换的情况下使用此方法:

cast(digits(cast(A.ivdat8 as dec(8))) || '000000' as date)

完整的where子句也不需要从sysibm.dummy1中选择。

where cast(digits(cast(A.ivdat8 as dec(8))) || '000000' as date) between current date - 3 days and current date

如果您的索引是基于ivdat8构建的,则最快的选择是:

where A.ivdat8 between cast(Current date - 3 days as dec(8)) and cast(Current Date as dec(8))

答案 2 :(得分:0)

设法将int转换为日期,首先我必须将其转换为字符串,然后使用TO_DATE

To_Date(cast(A.ivdat8 as varchar(50)),"YYYYMMDD")

答案 3 :(得分:0)

尝试

Where cast(TIMESTAMP_FORMAT(cast(ivdat8 as varchar(8)), 'YYYYMMDD') as date)                                                        
      Between (current date - 3 days) and current date
相关问题