Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Cast binary to char
Message
From
25/07/2005 13:10:56
 
General information
Forum:
Microsoft SQL Server
Category:
SQL syntax
Miscellaneous
Thread ID:
01035712
Message ID:
01035768
Views:
21
Thank you Sergey. Very nice concise code, it works very well, and it is very appreciated.

>>I am constructing a document number. It has several codes such as '05' of r fiscal year, and 'AB' for type, etc. It also has a two character counter imbedded in it, and it is space constrained (i.e. I am not able to expand the counter to three digits.) There needs to be room for 200 + items in any given set of numbers. Hexadecimal would take care of this, but I am not able to convert back and forth from binary to char. Currently there is a manual method that uses a table of alpha counters (AA, AB, AC, ... ZZ).
>>
>>Does that help?
>>
>
>Here's the code that will convert between integer and base36 in both directions
>DECLARE @i int, @cnt int, @Base36 char(36), @cnt36str char(2), @cntint int
>SET @cnt = 255
>SET @Base36 = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'
>
>-- Convert integer to base36 encoded 2 byte string
>SET @cnt36str = SUBSTRING(@Base36, (@cnt / 36)+1,1) +
>		SUBSTRING(@Base36, (@cnt % 36)+1,1)
>SELECT @cnt36str
>
>---
>
>-- Convert base36 encoded 2 byte string to integer
>SET @cntint = ( CHARINDEX(SUBSTRING(@cnt36str,1,1),@Base36) - 1) * 36 +
>		CHARINDEX(SUBSTRING(@cnt36str,2,1),@Base36) - 1
>SELECT @cntint
>
Thank You

Rollin Burr

Politicians and diapers have one thing in common. They should both be changed regularly, and for the same reason.
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform