Please be aware that the content in SAP SQL Anywhere Forum will be migrated to the SAP Community in June and this forum will be retired.

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

asked 28 Apr '10, 11:33

Breck%20Carter's gravatar image

Breck Carter
32.5k5417261050
accept rate: 20%

edited 28 Apr '10, 11:39


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
permanent link

answered 28 Apr '10, 11:38

Breck%20Carter's gravatar image

Breck Carter
32.5k5417261050
accept rate: 20%

Your answer
toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported

Question tags:

×17

question asked: 28 Apr '10, 11:33

question was seen: 2,168 times

last updated: 28 Apr '10, 11:39