SQL Server custom Conversion Function
Last Update:2018-12-03
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