SQL Server custom Conversion Function

Source: Internet
Author: User
Create Function bin_to_dec (@ v_bin varchar (32 ))
Returns int
As
Begin
Declare @ v_bin1 varchar (32)
Declare @ v_dec bigint
Declare @ v_com bigint
Declare @ v_temp_dec bigint
Declare @ v_temp_bin1 varchar (32)
Declare @ v_temp_bin2 varchar (1)
Declare @ v_n int
Declare @ v_bin2 varchar (32)
Set @ v_bin1 = Replace (@ v_bin, '0', '2 ')
Set @ v_bin2 = @ v_bin
Set @ v_dec = 0
Set @ v_temp_bin1 =''
Set @ v_temp_bin2 =''
Set @ v_n = 1
Set @ v_com = 0
While (@ v_bin1 <> @ v_com)
Begin
Set @ v_temp_bin2 = substring (@ v_bin1, 1, 1)
-- Print @ v_temp_bin2
-- Print @ v_bin1
If (@ v_temp_bin2 <> 1)
Set @ v_temp_bin2 = 0
Else set @ v_temp_bin2 = 1
Set @ v_temp_dec = @ v_temp_bin2 * power (2, Len (@ v_bin1)-1)
-- Print '@ v_temp_dec :'
-- Print @ v_temp_dec
Set @ v_n = @ v_n + 1
-- Print @ v_n
Set @ v_temp_bin1 = substring (@ v_bin2, @ v_n, Len (@ v_bin1)-1)
Set @ v_bin1 = @ v_temp_bin1
Set @ v_dec = @ v_dec + @ v_temp_dec
-- Print '@ v_dec'
-- Print @ v_dec
-- Print '------ ^ ------'
End
Return @ v_dec
Endgo create function hex_to_dec (@ v_hex varchar (32 ))
Returns int
As
Begin
Declare @ v_bin1 varchar (32)
Declare @ v_dec bigint
Declare @ v_com varchar (1)
Declare @ v_temp varchar (1)
Declare @ v_temp_dec bigint
Declare @ v_temp_bin1 varchar (32)
Declare @ v_temp_bin2 int
Declare @ v_n int
Declare @ v_bin2 varchar (32)
Set @ v_bin1 = Replace (@ v_hex, '0', 'x ')
Set @ v_bin2 = @ v_bin1
Set @ v_dec = 0
Set @ v_temp_bin1 =''
Set @ v_temp_bin2 =''
Set @ v_n = 1
Set @ v_com =''
While (@ v_bin1 <> '')
Begin
Set @ v_temp = substring (@ v_bin1, 1, 1)
-- Print @ v_temp_bin2
-- Print @ v_bin1
If (@ v_temp = 'X ')
Set @ v_temp_bin2 = 0
Else
If @ v_temp = 'A'
Set @ v_temp_bin2 = 10
Else
If (@ v_temp = 'B ')
Set @ v_temp_bin2 = 11
Else
If (@ v_temp = 'C ')
Set @ v_temp_bin2 = 12
Else
If (@ v_temp = 'D ')
Set @ v_temp_bin2 = 13
Else
If (@ v_temp = 'E ')
Set @ v_temp_bin2 = 14
Else
If (@ v_temp = 'F ')
Set @ v_temp_bin2 = 15
Else set @ v_temp_bin2 = @ v_temp



Set @ v_temp_dec = @ v_temp_bin2 * power (16, Len (@ v_bin1)-1)
-- Print '@ v_temp_dec :'
-- Print @ v_temp_dec
Set @ v_n = @ v_n + 1
-- Print @ v_n
Set @ v_temp_bin1 = substring (@ v_bin2, @ v_n, Len (@ v_bin1)-1)
Set @ v_bin1 = @ v_temp_bin1
Set @ v_dec = @ v_dec + @ v_temp_dec
-- Print '@ v_dec'
 
-- Print '------ ^ ------'
End
Return @ v_dec
-- Print @ v_dec
Endgo create function oct_to_dec (@ v_oct varchar (32 ))
Returns int
As
Begin
Declare @ v_bin1 varchar (32)
Declare @ v_dec bigint
Declare @ v_com bigint
Declare @ v_temp_dec bigint
Declare @ v_temp_bin1 varchar (32)
Declare @ v_temp_bin2 varchar (1)
Declare @ v_n int
Declare @ v_bin2 varchar (32)
Set @ v_bin1 = Replace (@ v_oct, '0', 'x ')
Set @ v_bin2 = @ v_oct
Set @ v_dec = 0
Set @ v_temp_bin1 =''
Set @ v_temp_bin2 =''
Set @ v_n = 1
Set @ v_com = 0
While (@ v_bin1 <> @ v_com)
Begin
Set @ v_temp_bin2 = substring (@ v_bin1, 1, 1)
-- Print @ v_temp_bin2
-- Print @ v_bin1
If (@ v_temp_bin2 = 'X ')
Set @ v_temp_bin2 = 0
Set @ v_temp_dec = @ v_temp_bin2 * power (8, Len (@ v_bin1)-1)
-- Print '@ v_temp_dec :'
-- Print @ v_temp_dec
Set @ v_n = @ v_n + 1
-- Print @ v_n
Set @ v_temp_bin1 = substring (@ v_bin2, @ v_n, Len (@ v_bin1)-1)
Set @ v_bin1 = @ v_temp_bin1
Set @ v_dec = @ v_dec + @ v_temp_dec
-- Print '@ v_dec'
-- Print @ v_dec
-- Print '------ ^ ------'
End
Return @ v_dec
Endgo
Related Article

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.