as400日期查询
要在 as400 中查询从今天开始的最后 7 天,因为它将日期存储在 char 类型中,如何检索今天的结果,因为我尝试使用诸如
where chardate >= char(days(curdate()) + 7)
但它仍然不起作用
To query the last 7 days from today in as400 as it stores the dates in char type how to retrieve the results from today as i tried using such as
where chardate >= char(days(curdate()) + 7)
but its still not working
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
答案分为两部分。第一个涉及 DB2 的这种特殊风格的日期数学。相当于
curdate()) + 7
的 DB2 表达式是当前日期 + 7 天
。第二个涉及将日期值转换为字符值(反之亦然),以便我们可以比较它们。在真正破解日期之前,我们需要知道
chardate
存储日期的格式。我们假设它采用YYYY-MM-DD
格式。然后,您可以使用 char(current date + 7 days, iso) 来以相同的格式获取未来 7 天。因此,根据这个假设,您的 where 语句将是
date
可以转换为几种标准日期格式:YYYY-MM-DD
MM /DD/YYYY
DD.MM.YYYY
YYYY-MM-DD
如果您的
chardate
位于不同的格式,您将需要使用substr
进行一些相当繁琐的工作。例如,要将YYYY-MM-DD
转换为YYYYMMDD
,您需要类似的内容此方法的一个主要问题是格式不存储在“年月”中天”订单无法进行可靠比较。也就是说,
12311969
(即MMDDYYYY
)将比较大于01012011
,因为就数据库而言,您正在比较两个八位数字。 (这就是为什么您几乎应该始终将日期存储在实际的date
字段或YYYYMMDD
或类似的正确排序格式中。)我使用名为 idate,它提供 SQL 用户定义函数 (UDF) 来转换存储在 char 和 date 中的日期。数字字段转换为日期。请注意,此解决方案需要 RPG 编译器的可用性。
There are two parts to the answer. The first involves date math on this particular flavor of DB2. The DB2 expression equivalent to
curdate()) + 7
iscurrent date + 7 days
.The second involves converting date values to character values (or vice versa) so we can compare them. We need to know what format
chardate
stores dates in before we can really crack that one. Let's assume it's inYYYY-MM-DD
format. Then you can usechar(current date + 7 days, iso)
to get seven days in the future in the same format.So, with that assumption, your where statement would be
There are several standard date formats that
date
can convert to:YYYY-MM-DD
MM/DD/YYYY
DD.MM.YYYY
YYYY-MM-DD
If your
chardate
is in a different format, you will need to do some rather fiddly work withsubstr
. For example, to convertYYYY-MM-DD
toYYYYMMDD
you'd need something likeA major problem with this method is that formats that aren't stored in "year month day" order can't be reliably compared. That is,
12311969
(i.e.,MMDDYYYY
) will compare as greater than01012011
, since as far as the database is concerned, you're comparing two eight-digit numbers. (That's why you should almost always store dates in actualdate
fields or inYYYYMMDD
or similar properly ordered format.)I've had great success using a free utility called idate, which provides SQL user-defined functions (UDFs) to convert dates stored in char & numeric fields into dates. Note that this solution requires the availability of an RPG compiler.
从今天起的最后 7 天:
要执行字符范围比较:
For the last 7 days from today:
To perform a character range comparison: