Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Cast binary to char
Message
De
25/07/2005 13:10:56
 
 
Information générale
Forum:
Microsoft SQL Server
Catégorie:
Syntaxe SQL
Divers
Thread ID:
01035712
Message ID:
01035768
Vues:
20
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.
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform