It would be nice to have support to be able to cast short binary types into primative number types.
create table test
(
test1 BINARY(2)
)
select cast(test1 as SMALLINT) from test
select cast(test1 as BIGINT) from test
I would suggest that restriction is placed on BINARY size to avoid overflows/meaningless conversions
create table test
(
test1 BINARY(8)
)
select cast(test1 as SMALLINT) from test should return an error.
Obviously endianness would be a conern so a function to reverse endianness would be helpful too. i.e. SwapBytes().
Also ability to handle BINARY of odd sizes.
e.g. create table test
(
test 1 BINARY(3)
)
select cast(test1 as BIGINT) from test from should work.
|
Please download the latest package, which has provide these functions below:
BToInt_LE(binary): get int value from bytes with little-endian.
BToInt_BE(binary): get int value from bytes with big-endian.
IntToB_LE(binary): get bytes with little-endian from int value.
IntToB_BE(binary): get bytes with big-endian from int value.
BToShort_LE(binary): get short value from bytes with little-endian.
BToShort_BE(binary): get short value from bytes with big-endian.
ShortToB_LE(binary): get bytes with little-endian from short value.
ShortToB_BE(binary): get bytes with big-endian from short value.
BToLong_LE(binary): get long value from bytes with little-endian.
BToLong_BE(binary): get long value from bytes with big-endian.
LongToB_LE(binary): get bytes with little-endian from long value.
LongToB_BE(binary): get bytes with big-endian from long value.
Tested sqls:
create table test ( test1 BINARY(2) )
select btoshort_be(test1) from test
select btolong_be(test1) from test
create table test1 ( test1 BINARY(8))
select btoshort_be(test1) from test1
create table test2 ( test1 BINARY(3) );
select btolong_be(test1)from test2
|