SQLCODE -171: Invalid argument to a function
SQLCODE -171 (SQLSTATE 42815) means the data type, length or value of one argument of a scalar function is invalid. The message gives the function name and the position of the bad argument.
Other names for this code
Also written as SQL -171, SQLCODE=-171, SQLCODE = -171, db2 -171, sqlstate 42815, SQL0171N, data type length or value of argument is invalid.
What it means
Each built-in function in Db2 accepts certain data types, lengths and values for each argument. DECIMAL needs a precision and scale that make sense. CHAR and VARCHAR formats need a valid format keyword. String functions need string arguments. When an argument breaks those rules, Db2 returns -171 and gives the argument's position and the function's name.
The position can be off by one from what you see. Some functions have optional arguments that Db2 fills in for you, and IBM's documentation counts those. In TRIM('12' FROM '123'), for example, the implicit BOTH is argument 1, so a problem with '12' is reported as argument 2.
The Db2 12 for z/OS codes add a few specific cases. In EBCDIC or ASCII data, a character argument can't be used where a graphic one is required, or the other way round. Only Unicode allows mixing them. A string function that needs a string unit such as CODEUNITS16, CODEUNITS32 or OCTETS didn't get one. The RID function needs an exposed table name. And a newer function form may need a higher APPLCOMPAT level than the package or application uses. One case has its own SQLSTATE. An invalid BSON object gives 22546 instead of 42815.
Common causes
- A numeric argument passed to a function that wants a string, or a string passed to one that wants a number, where Db2 doesn't convert it.
- A length, precision or scale argument that's out of range, for example in DECIMAL or VARCHAR.
- A format or keyword argument that the function doesn't accept, such as an unsupported format in CHAR or a date-time function.
- Mixing character and graphic strings in a function on EBCDIC or ASCII data.
- Using a function form that needs a newer APPLCOMPAT level than the package was bound with.
How to find the problem
- Read the function name and argument position in the
DSNT408Imessage or SQLERRMC. - Count the arguments as IBM defines them, including implicit ones, then compare that argument's data type and value with the function description in the SQL Reference for your release.
- If the argument is a column, check its type and length in SYSIBM.
SYSCOLUMNS. If it's a host variable, check how the program declares it. - For a function that works elsewhere, compare the APPLCOMPAT value of the package in SYSIBM.
SYSPACKAGE, or of the CURRENT APPLICATION COMPATIBILITY register for dynamic SQL.
How to fix it
- Pass the argument in the type the function expects, using CAST if needed.
- Correct lengths, precisions, scales and format keywords to values the function accepts.
- Add the string unit the function requires, such as CODEUNITS32.
- Rebind with a higher APPLCOMPAT, or set the special register for dynamic SQL, if the function form needs a newer level.
- Test the corrected call in a query tool before changing the program.
What it looks like
DSNT408I SQLCODE = -171, ERROR: THE DATA TYPE, LENGTH, OR VALUE OF ARGUMENT 2 OF DECIMAL IS
INVALID
DSNT418I SQLSTATE = 42815 SQLSTATE RETURN CODEIBM documentation
IBM documentation: -171. Use the documentation for your installed release.
Related codes
Questions
Why does the error name argument 2 when my first argument is wrong?
Db2 may count an implicit argument you didn't write, such as BOTH in TRIM. Check the function's full syntax to see how its arguments are numbered.
Why does the same SQL work on another Db2 system?
The data types of the columns may differ, or the package may have a different APPLCOMPAT level. Some function forms need a newer level.
Is it the same on Db2 for LUW?
Yes. SQL0171N has SQLSTATE 42815 and reports a bad argument of a routine in the same way.