I've tried you approach.. but it seems I can't use this casting approach in a non-unicode db..
Original Message:
Sent: Thu March 14, 2024 01:49 PM
From: Roland Schock
Subject: JSON_OBJECT returning SQL0171N error when it shouldn't
Hi Samuel,
I was pondering this afternoon about your issue and searched a bit my old documentation folder.
In 2018 IBM announced Db2 11.1.4.4. @George Baklarz presented at IDUG in Malta about the new changes for JSON (On the IDUG website his presentation would be free to download, but currently this is not so easy. so I attach it here).
If you look at pages 3..5 you can see, the state of the JSON functions at that time. IBM implemented something and then the standard got finalized and IBM adopted the new standard then. The older functions in SYSTOOLS schema are still available. But probably the new functions got signatures for the parameters to accept UTF-8 only (=this is my educated guess here).
So an idea is to cast the strings you are using with the newer functions accordingly. I cannot try it myself, as I would have to setup an instance with such an old code first. In the example below, you can see also some additional options I have found in the docs. Either UNICODE_STR() works or probably also CAST('Last name' AS VARCHAR(30) CODEUNITS32).
The following example was running without error on my Ubuntu test system:
db2 "SELECT JSON_OBJECT(KEY UNICODE_STR('Last name') VALUE LASTNAME returning varbinary(40) format json encoding utf8) FROM EMPLOYEE WHERE EMPNO = '000020'"
I could reproduce the error message, when I used invalid combinations, like
db2 "SELECT JSON_OBJECT(KEY UNICODE_STR('Last name') VALUE LASTNAME returning varchar(40) format bson) FROM EMPLOYEE WHERE EMPNO = '000020'"
as the binary BSON format cannot be converted to VARCHAR. It must be a binary format with BSON. I got the same error:
SQL0171N The statement was not processed because the data type, length or
value of the argument for the parameter in position "3" of routine
"SYSIBM.JSON_OBJECT" is incorrect. Parameter name: "". SQLSTATE=42815
It is a generic error for the incompatible parameters in these functions.
Maybe casting can help you to extend the time before you have to convert the database to the new format with UTF-8.
Cheers
------------------------------
Roland Schock
IBM Champion and IBM Gold Consultant
Original Message:
Sent: Thu March 14, 2024 08:37 AM
From: Samuel Pizarro
Subject: JSON_OBJECT returning SQL0171N error when it shouldn't
Hi Jan
Ok, thanks for the confirmation about utf-8 requirements.. so based on this. don't you agree that those functions should return a better error message instead of this unexpected one, which is pretty hard to guess what's happening ?
I hope someone from IBM agrees with that.
--------
About the reload.. the database is very active.. but the customer will have to choose.. if they want to start using json built-in stuff, they will have the offer an offline window. We will start doing some tests.. to best estimate the total required time to re-load this.
Appreciate everyone's assistance on this.
Regards
------------------------------
Samuel Pizarro
Original Message:
Sent: Thu March 14, 2024 04:48 AM
From: Jan Nelken
Subject: JSON_OBJECT returning SQL0171N error when it shouldn't
Samuel,
As indicated by Roland - since Db2 V11.1.4 - JSON related build-in function do require UTF-8 database:

The default code set for a database is UTF-8.
USING CODESET codeset
Specifies the code set to be used for data entered into this database. After you create the database, you cannot change the specified code set.
TERRITORY territorySpecifies the territory identifier or locale identifier to be used for data entered into this database. After you create the database, you cannot change the specified territory. The combination of the code set and territory or locale values must be valid.
Your only option is to reload database.
Strategy could be to create a new database, then LOAD from cursor from old database. You may also want to consider obtaining HPU license for the migration phase.
How active is your database? How long you can afford to keep old database offline?
May I suggest to pay attention at the planning phase of future migration(s) to warning and notes like this (referring to AUTOMATIC STORAGE parameter values):

------------------------------
Jan Nelken
Original Message:
Sent: Wed March 13, 2024 08:34 PM
From: Samuel Pizarro
Subject: JSON_OBJECT returning SQL0171N error when it shouldn't
yes.. the db was for sure properly migrated.. otherwise you can't really run the db without it.. it's mandatory.
DB is about 2TB. I don't have HPU unload, it's not a built-in feature..
Digging further, this db was so old that it was not created as UNICODE/UTF-8 db. I believe this might be the issue. I am not finding the proper pre-requirements for json functionality.. but I vaguely remember something about this.. but I am really not sure.
As a mater of fact, if unicode is mandatory, there should be a better error than this.. if the pre-req is not met.. and someone tries to use the functions.
That's a terrible error message if this is really the case.
Can someone from ibm confirm this ? I was really not able to find this statement in ibm db2 docs webpages.
And if that is true, I guess I will really have to recreate db from scratch and re-laod it !!!
OUCH!! IBM should really provide a better way of changing the database code-page/collation on the fly. Several other DBMS offers that..
Regards
------------------------------
Samuel Pizarro
Original Message:
Sent: Wed March 13, 2024 10:25 AM
From: Jan Nelken
Subject: JSON_OBJECT returning SQL0171N error when it shouldn't
And one more question:
at each stage (major version change, minor version) you did run db2 migrate database xxx?
How big is this database?
Is it feasible to create a test database and reload data (HPU unload -> LOAD) or EXPORT -> LOAD?
------------------------------
Jan Nelken
Original Message:
Sent: Wed March 13, 2024 08:19 AM
From: Samuel Pizarro
Subject: JSON_OBJECT returning SQL0171N error when it shouldn't
I don't know exactly "when" the issue started.. this is the 1st time we are trying to use JSON functionality on this old db.
It could have happen at any previous migration path.. 10.1 to 10.5, 10.5 to 11.1, etc ...
But yes, we have executed the corresponding db2updvXXX on each of them.
Just in case, I've just re-executed it , the db2updv115 returns ok, completed successfully. but the json_object issue/error is still happening.
Nothing gets fixed.
Regards
------------------------------
Samuel Pizarro
Original Message:
Sent: Wed March 13, 2024 03:31 AM
From: Jan Nelken
Subject: JSON_OBJECT returning SQL0171N error when it shouldn't
After migration to Db2 V11.5 - you did run db2updt115, right?
------------------------------
Jan Nelken
Original Message:
Sent: Tue March 12, 2024 04:56 PM
From: Samuel Pizarro
Subject: JSON_OBJECT returning SQL0171N error when it shouldn't
db2 11.5 on windows, JSON_OBJECT fails to execute with SQL0171N no matter what.
db2 "select json_object (key 'id' value 1) from sysibm.sysdummy1"SQL0171N The statement was not processed because the data type, length orvalue of the argument for the parameter in position "1" of routine"SYSIBM.JSON_OBJECT" is incorrect. Parameter name: "". SQLSTATE=42815
we first notice id trying to create a json_object from a decimal column. but no matter what we try, it fails out with the same error.
Even running the sample code from the doc page
JSON_OBJECT scalar function - IBM Documentation
db2 "VALUES JSON_OBJECT(KEY 'first' VALUE 'John', KEY 'last' VALUE 'Doe')"SQL0171N The statement was not processed because the data type, length orvalue of the argument for the parameter in position "1" of routine"SYSIBM.JSON_OBJECT" is incorrect. Parameter name: "". SQLSTATE=42815
This db is currently at 11.5 release, but this db was not originally created under this version. it is an old db that has been upgraded several times.. 10.x for sure.
If we create a sample db, and try the same everything works. We need assistance to fix this issue.
this is our current level
db2levelDB21085I This instance or install (instance name, where applicable: "DB2")uses "64" bits and DB2 code release "SQL11058" with level identifier"0609010F".Informational tokens are "DB2 v11.5.8000.317", "s2209201700","DYN2209201700WIN64", and Fix Pack "0".Product is installed at "C:\PROGRA~1\IBM\SQLLIB" with DB2 Copy Name "DB2COPY1".
Appreciate any kind of support, specially from IBM as this seems to be something wrong with json functions uppon db migrations.
IBM please , help !
------------------------------
Samuel Pizarro
------------------------------