DB2 - Argument 02 of function TRANSLATE not valid

走远了吗. 提交于 2019-12-12 03:46:58

问题


I am having some issues converting a string (yyyymmddhhiiss) to a date using TRANSLATE.

If I use a string directly then it works perfectly fine, but when i use a field of exactly same datatype, varchar(14), then it throws the error from the title.

Here is a basic example of what i am trying to do:

WITH test_table AS (
    SELECT '20160101123059' AS d FROM SYSIBM.SYSDUMMY1
)
SELECT d
       , translate('ABCD-EF-GH IJ:KL:MN', d, 'ABCDEFGHIJKLMN')
       , translate('ABCD-EF-GH IJ:KL:MN', '20160101123059','ABCDEFGHIJKLMN')
  FROM test_table

Can one of you explain why this is not working? Thanks.


回答1:


From the DB2 for i manual...

to-string
A string that specifies the characters to which certain characters in expression are to be converted. This string is sometimes called the output translation table. The string must be any built-in numeric or string constant.

So it won't work the way you're trying to use it.

Argument 2 must be a constant value.

Assuming a supported release of the IBM i, you should be able to use the timestamp() function to convert the 14-character string directly to timestamp.

select timestamp('20160101123059')
from sysibm.sysdummy1             



回答2:


You cas use : TIMESTAMP_FORMAT('20160101123059' , 'YYYYMMDDHH24MISS')

If you want a char to result: cast(TIMESTAMP_FORMAT('20160101123059' , 'YYYYMMDDHH24MISS') as varchar(50))



来源:https://stackoverflow.com/questions/37317263/db2-argument-02-of-function-translate-not-valid

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!