Author |
Message
|
jamesyu |
Posted: Fri May 11, 2007 7:16 am Post subject: Supported casts - BLOB to Integer |
|
|
Acolyte
Joined: 31 Jan 2007 Posts: 70
|
Hi All,
In the v6 ESQL manual -> Supported Casts, it documents that BLOB can be casted to Integer, quote:
"The byte array has a maximum of 263 elements and is converted to an integer. An error is reported if the source is not of the correct length to match an integer."
What is the last statement of "An error is reported if the source is not of the correct length to match an integer" supposed to mean?
Now I am trying to convert, say a hex value of X'3412' to an integer with the following code but failed with an exception saying "Unsuitable source lenght" returned. Does anyboy know why?
DECLARE x BLOB;
DECLARE y INT;
SET x = X'3412';
SET y = CAST(x AS INT);
Thanks,
James |
|
Back to top |
|
 |
jefflowrey |
Posted: Fri May 11, 2007 7:20 am Post subject: |
|
|
Grand Poobah
Joined: 16 Oct 2002 Posts: 19981
|
Specify an Encoding on the CAST. _________________ I am *not* the model of the modern major general. |
|
Back to top |
|
 |
jamesyu |
Posted: Fri May 11, 2007 9:09 am Post subject: |
|
|
Acolyte
Joined: 31 Jan 2007 Posts: 70
|
jefflowrey wrote: |
Specify an Encoding on the CAST. |
Tried it with different encodings:
SET y = CAST(x AS INT ENCODING 1); -- MQENC_INTEGER_NORMAL
SET y = CAST(x AS INT ENCODING 16); -- MQENC_DECIMAL_NORMAL
SET y = CAST(x AS INT ENCODING InputRoot.MQMD.Encoding);
None of those works. Can you please advise again, or any sample lines?
Thanks,
James |
|
Back to top |
|
 |
mgk |
Posted: Fri May 11, 2007 9:59 am Post subject: |
|
|
 Padawan
Joined: 31 Jul 2003 Posts: 1642
|
Hi.
The phrase
Quote: |
"correct length to match an integer" |
means that in order to be CAST to an INT the BLOB must be of the same length as an integer, which is 8 bytes (as in ESQL INTs are 64 bits). Unfortuantely, it does not automatically pad with 0 if the input value is < 8 so you will have to do that.
Regards, _________________ MGK
The postings I make on this site are my own and don't necessarily represent IBM's positions, strategies or opinions. |
|
Back to top |
|
 |
jamesyu |
Posted: Fri May 11, 2007 10:17 am Post subject: |
|
|
Acolyte
Joined: 31 Jan 2007 Posts: 70
|
mgk wrote: |
Hi.
The phrase
Quote: |
"correct length to match an integer" |
means that in order to be CAST to an INT the BLOB must be of the same length as an integer, which is 8 bytes (as in ESQL INTs are 64 bits). Unfortuantely, it does not automatically pad with 0 if the input value is < 8 so you will have to do that.
Regards, |
Ok, I tried this with the same error returned:
DECLARE x BLOB;
SET x = X'00003412';
DECLARE y INT;
SET y = CAST(x AS INT);
Any ideas?
Thanks,
James |
|
Back to top |
|
 |
EddieA |
Posted: Fri May 11, 2007 10:21 am Post subject: |
|
|
 Jedi
Joined: 28 Jun 2001 Posts: 2453 Location: Los Angeles
|
jefflowrey wrote: |
Specify an Encoding on the CAST. |
Cheers, _________________ Eddie Atherton
IBM Certified Solution Developer - WebSphere Message Broker V6.1
IBM Certified Solution Developer - WebSphere Message Broker V7.0 |
|
Back to top |
|
 |
mgk |
Posted: Fri May 11, 2007 10:51 am Post subject: |
|
|
 Padawan
Joined: 31 Jul 2003 Posts: 1642
|
Hi,
It is failing because you are only passing 4 bytes, not 8 to the CAST.
Your statement
Code: |
SET x = X'00003412'; |
only creates a BLOB
that is 4 bytes long (as each pair of chars represents one byte). You can check this by using the LENGTH function on the BLOB and inspecting the result.
In this case, to make the CAST work, you need to pad to make 8 bytes with 8 more zeros as in:
Code: |
SET x = X'0000000000003412'; |
.
The ENCODING is not relavent in this case. _________________ MGK
The postings I make on this site are my own and don't necessarily represent IBM's positions, strategies or opinions. |
|
Back to top |
|
 |
jamesyu |
Posted: Mon May 14, 2007 5:46 am Post subject: |
|
|
Acolyte
Joined: 31 Jan 2007 Posts: 70
|
|
Back to top |
|
 |
|