From: "Jim Diaz" Newsgroups: sybase.public.sqlanywhere.general Subject: Store Series of SMALLINT as Binary Date: 27 Apr 2010 16:35:35 -0700 ASA 9.0.2 most recent EBF I'm trying to store a series of SmallInt as binary data in a procedure but no matter how I try to store it takes 4 bytes DECLARE @Result LONG BINARY; DECLARE @SIValue SMALLINT; SET @SIValue = 2; SET @Result = @SIValue; -- DATALENGTH(@Result) RETURNS 4; SET @Result = CAST(@SIValue AS BINARY); -- DATALENGTH(@Result) RETURNS 4; SET @Result = CAST(@SIValue AS SMALLINT); -- DATALENGTH(@Result) RETURNS 4; Thanks for the help. Jim |
The SQL Anywhere rules for implicit data type conversions are not well documented. It looks like SMALLINT values are converted to BINARY as if they were INTEGER. Here is a kludge to force a SMALLINT to be treated like a SMALLINT; note that INTTOHEX does the same thing (treats SMALLINT as if it was INTEGER), thus requiring RIGHT() to render the desired result: BEGIN DECLARE @Result LONG BINARY; DECLARE @SIValue SMALLINT; SET @SIValue = 95; SET @Result = @SIValue; EXECUTE IMMEDIATE STRING ( 'SET @Result = 0x', RIGHT ( INTTOHEX ( @SIValue ), 4 ) ); SELECT INTTOHEX ( @SIValue ), @Result, DATALENGTH( @Result ), CAST ( @Result AS SMALLINT ) ; END; INTTOHEX(@SIValue),@Result,DATALENGTH(@Result),@Result '0000005f',0x005f,2,95 |