简体   繁体   中英

to_date with AS400

I am writing to you because I can't use the operator to_date on an AS400 database.

With Oracle database, I use:

datefield >= to_date('01/01/2014','DD/MM/YYYY')

But with AS400, I get an error:

Incompatible operator

Is there another function I may use to replace to_date ?

assuming datefield is a actual date data type

Then all you need to do is use an ISO formatted date string

datefield >= '2014-01-01'

DB2 for IBM i will always recognize '2014-01-01' as a date.

But if you really want to explicitly convert it yourself, then there's two functions

DATE('2014-01-01')
CAST('2014-01-01' as DATE)

CAST is preferred for portability.

I recommend sticking with ISO format, though the system will recognize USA 'mm/dd/yyyy' and EUR 'dd.mm.yyyy'.

Reference here:

http://www-01.ibm.com/support/knowledgecenter/ssw_ibm_i_71/db2/rbafzdtstrng.htm

I realize this topic is old , but the current answer seemed mostly to ignore the original issue with TO_DATE, and instead offer a circumvention; of course the circumvention is IMO, a better approach. By addition of the message identifier and further explanation of the original issue and possible resolutions, hopefully those are beneficial to others in both locating this discussion as a match to their own issue and beneficial for the additional commentary provided.

The issue described in the OP is a reflection of the error condition SQL0401 [sqlcode -401] diagnosing that the data-type of the TO_DATE scalar is a TIMESTAMP whereas the DateField column data-type is a DATE [or so implied, although if the OP had included the DDL for the TABLE, the reviewers could be assured that "datefield" is indeed a column of the DATE data type].

In v5r3 the "Cause" is described by the text "Date, time, and timestamp operands are compatible with character operands or with another operand of the same type."; FWiW the USEnglish [first-level] text likely would have been "Comparison operator >= operands not compatible.", rather than just "Incompatible operator" as was noted in the OP. Even by v7r1, the documentation suggests no change for the SQL0401:
http://www.ibm.com/support/knowledgecenter/ssw_ibm_i_71/rzala/rzalaml.htm

"...
Date, time, and timestamp operands are compatible with character and graphic operands or with another operand of the same type.
..."

Despite the name of the scalar function, for what might seem the logical effect given that moniker, the scalar result is not a DATE data type; the effect is instead reflective of the scalar function name TIMESTAMP_FORMAT, thus yielding a TIMESTAMP scalar result. The moniker TO_DATE is merely a synonym\\syntax-alternative:
http://www.ibm.com/support/knowledgecenter/api/content/ssw_ibm_i_71/db2/rbafzscatsformat.htm

The originally described scenario datefield >= to_date('01/01/2014','DD/MM/YYYY') for which a non-standard date format is coded, the error could be prevented by explicitly casting the result of that TO_DATE scalar to a DATE type. For example by wrapping the TO_DATE result in another [casting] scalar, such as for example, either of the DATE casting scalar
datefield >= DATE(to_date('01/01/2014','DD/MM/YYYY'))
or the CAST scalar
datefield >= CAST(to_date('01/01/2014','DD/MM/YYYY') as DATE)

Of course the other alternatives of using a character-string formatted as one of the standards date formats [eg *ISO as Charles suggested] is probably just as simple; even if that usage is not as explicitly revealing [to a reviewer of the statement] as would be the format-string specified as the second argument on the TO_DATE(). But per the specification originally shown as 'DD/MM/YYYY', the preference may be to use the *EUR standard formatting for which the format is 'DD.MM.YYYY'; ie coded as datefield >= '01.01.2014'

Note that in addition to the Date strings documentation reference http://www.ibm.com/support/knowledgecenter/ssw_ibm_i_71/db2/rbafzdatestrings.htm , there is another alternative not mentioned on that page which is a somewhat redundant form of the *ISO formatted character-string, DATE '2014-01-01' and almost the same [both in specification and redundancy; the alternative merely saves typing the parentheses] as the DATE [casting] scalar specification DATE('2014-01-01') already mentioned elsewhere in this topic. Thus each of datefield >= DATE'2014-01-01' or datefield >= '2014-01-01' or datefield >= DATE('2014-01-01') are all equivalent, and each is depending on *ISO formatting of the character-string as the date representation.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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