SQL decimal to binary and sixteen functions

Source: Internet
Author: User
Tags decimal to binary

Decimal to binary and sixteen functions
A. Call the test:
Update mysheet1 set binary = DBO. inttobit (decimal)
Update mysheet2 set hexadecimal = DBO. inttohex (decimal)
B. function content
-- Decimal to binary function
Create Function DBO. inttobit (@ number INT)
Returns varchar (100)
As
Begin
Declare @ I int
Declare @ J float
Declare @ M int
Declare @ out1 varchar (1)
Declare @ out2 varchar (20)
Set @ I = @ number
Set @ out2 =''
While @ I> = 1
Begin
Set @ J = @ I/2
Set @ M = @ I % 2
Set @ I = floor (@ J)
Set @ out1 = cast (@ M as char (1 ))
Set @ out2 = @ out1 + @ out2
End
Return @ out2
End

-- Convert decimal to hexadecimal Functions
Create Function DBO. inttohex (@ I INT)
Returns varchar (15)
Begin
-- Declare @ I int
-- Set @ I = 11259375
Declare @ r varchar (10)
Set @ r =''
While @ I/16> 0
Begin
Set @ r =
(Case
When (@ I % 16) <= 9 then convert (varchar (1), @ I % 16)
When (@ I % 16) = 10 then 'A'
When (@ I % 16) = 11 then 'B'
When (@ I % 16) = 12 then 'C'
When (@ I % 16) = 13 then 'D'
When (@ I % 16) = 14 then 'E'
When (@ I % 16) = 15 then 'F'
End)
+ @ R
-- Select @ r, @ I
Set @ I = @ I/16
End
-- Select @ r, @ I
If @ I> 0
Set @ r = (Case
When (@ I % 16) <= 9 then convert (varchar (1), @ I % 16)
When (@ I % 16) = 10 then 'A'
When (@ I % 16) = 11 then 'B'
When (@ I % 16) = 12 then 'C'
When (@ I % 16) = 13 then 'D'
When (@ I % 16) = 14 then 'E'
When (@ I % 16) = 15 then 'F'
End) + @ r
-- Select @ r
Return @ r
End

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.