简体   繁体   English

IBM i(AS400 / ISeries)-在WRKQRY中添加日期到日期字段

[英]IBM i (AS400/ISeries) - Adding days to date field in WRKQRY

I have a decimal date field (TDDATR) that is in the YYYYMMDD format. 我有一个YYYYMMDD格式的十进制日期字段(TDDATR)。

I would like to create a field that is TDDATR + 30 days but I am unable to. 我想创建一个TDDATR + 30天的字段,但是我不能。

Using 'Define Results Field' I have tried a few things; 使用“定义结果字段”,我尝试了一些方法;

  1. Simply doing this; 只需这样做;

TDDATR + 30 DAYS TDDATR + 30天

But it returned this error: Labeled duration not used correctly. 但它返回此错误: 标记的持续时间未正确使用。

  1. I tried using the DIGITS and SUBSTR commands to create a field in the DDMMYYYY format and then +30 days but got the same error. 我尝试使用DIGITS和SUBSTR命令以DDMMYYYY格式创建一个字段,然后再添加30天,但出现相同的错误。

  2. Same as above but in the DD/MM/YYYY format - same error. 与上述相同,但格式为DD / MM / YYYY-错误相同。

  3. Using DATE(TDDATR) but all I see is +'s in the field. 使用DATE(TDDATR),但我只看到该字段中的+。

  4. Using DATE( ) on the fields created in step 2 and 3 - still get +'s 在第2步和第3步创建的字段上使用DATE()-仍为+

I've ran out of ideas - any help would be greatly appreciated. 我没有想法-任何帮助将不胜感激。

Query/400 lacks a lot of the features that an SQL based interface has. Query / 400缺少基于SQL的界面所具有的许多功能。

I'd urge you to consider switching to Query Manager (STRQM) which is a fully SQL based product. 我敦促您考虑切换到查询管理器(STRQM),它是完全基于SQL的产品。 You can even convert Query/400 queries to Query Manager queries with the RTVQMQRY command by having the ALWQRYDFN parm set to *YES. 通过将ALWQRYDFN参数设置为* YES,甚至可以使用RTVQMQRY命令将Query / 400查询转换为Query Manager查询。

The other option that IBM is pushing is Web Query. IBM推出的另一个选项是Web Query。 Again, fully SQL based and you can convert Query/400 queries into it. 同样,完全基于SQL,您可以将Query / 400查询转换为SQL。

Having said that, the problem is that FLD + 30 DAYS only works when FLD is a DATE data type. 话虽这么说,问题是FLD + 30天仅在FLD是DATE数据类型时才有效。 Query/400 includes a DATE() function to convert non-date types into date. Query / 400包括一个DATE()函数,用于将非日期类型转换为日期。 But it's very limited in that it only works with character fields formatted according to your job defaults. 但这是非常有限的,因为它仅适用于根据您的工作默认值设置格式的字符字段。 Assuming you're in the US, it'd only work with a character value of '07/01/15'. 假设您在美国,则只能使用字符值'07 / 01/15'。

You could do a lot of manipulation in Query/400 and end up with a result field that meets DATE()'s requirements. 您可以在Query / 400中进行很多操作,最后得到一个满足DATE()要求的结果字段。 But a better solution would be to create an SQL view over your table and have your numeric date converted into a date data type in the view. 但是更好的解决方案是在表上创建一个SQL视图,并将数字日期转换为视图中的日期数据类型。

You can find code examples that show how to convert a numeric YYYYMMDD to a actual date data type in the view. 您可以找到代码示例,这些代码示例在视图中显示了如何将数字YYYYMMDD转换为实际日期数据类型。 However, I'd recommend create a user defined function (UDF) that will do the conversion for you. 但是,我建议创建一个用户定义函数(UDF),该函数将为您完成转换。 That will make it much easier to use in the view and to reuse in other places. 这将使在视图中使用和在其他地方重用变得更加容易。

If you'd like, there's an open source package called iDate , that includes all the code required for convert to/from date data types. 如果您愿意的话,有一个名为iDate的开源软件包,其中包含转换为/自日期数据类型所需的所有代码。

Download that, install/compile it and your SQL view becomes 下载它,安装/编译它,您的SQL视图将变为

select ... idate(TDDATR,'*CCYMD') as TD_DATE
from myfile

The use of days is as follow 使用天数如下

Field       Expression                       
CURDATE_30  days(current(date)) + 30         

The solution to your problem is: given the field A dec(8,0) 您的问题的解决方案是:给定字段A dec(8,0)

Field       Expression                       
YYYYMMDD_   date(substr(digits(a),5,2)||'/'||
             substr(digits(a),7,2)||'/'||
             substr(digits(a),3,2))      

NEXT_MONTH  DAYS(YYYYMMDD_) + 30              

Remember to check the date format in your job description. 切记检查您的职位描述中的日期格式。 In the example the format is MDY or MM/DD/YY. 在示例中,格式为MDY或MM / DD / YY。

More info here 更多信息在这里

Based on the information here , I created the below 2 fields; 根据此处的信息,我创建了以下2个字段;

TDDIGI  DIGITS(TDDATR)           

TDDAT1  SUBSTR(TDDIGI,7,2)||'/'||
        SUBSTR(TDDIGI,5,2)||'/'||
        SUBSTR(TDDIGI,3,2)           

From here I was able to create a date field; 从这里我可以创建一个日期字段;

TDDAT2      DATE(TDDAT1)

Which allowed me to perform the necessary calculations. 这使我能够执行必要的计算。

The format of TDDAT1 is based on your job description which can be found by; TDDAT1的格式基于您的职位描述,可以通过以下方式找到:

  • WRKJOB WRKJOB
  • Option 2 选项2
  • Page down 向下翻页
  • Date format..: X 日期格式..:X

Mine was *DMY, so TDDAT1 was formatted based on this. 我的是* DMY,因此TDDAT1是基于此格式的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM