Mutual conversion between decimal and October (this method is applied to all hexadecimal and 10hexadecimal conversions)

Source: Internet
Author: User
------ Convert decimal to eighteen bytes
Create Function f_int18 (@ num INT)
Returns nvarchar (50)
As
Begin
If @ num = 0
Return '0'
Declare @ s nvarchar (50)
Set @ s =''
While @ num> 0
Select @ s = substring ('0123456789abcdefhg ', @ num % 18 + 1, 1) + @ s, @ num = @ num/18
Return @ s
End

Go
---------- Eighteen bytes to decimal
Create Function f_int10 (@ s nvarchar (50 ))
Returns int
As
Begin
Declare @ I int, @ S2 nvarchar (2), @ num int
Select @ I = Len (@ s), @ num = 0
While @ I> 0
Select @ S2 = substring (reverse (@ s), @ I, 1 ),
@ Num = power (18, @ i-1) * (charindex (@ S2, '0123456789abcdefhg ')-1) + @ num,
@ I = @ I-1
Return @ num
End
Go


Select DBO. f_int18 (9999)
Select DBO. f_int10 ('1cf9 ')

/**//*
--------------------------------------------------
1cf9

(The number of affected rows is 1)


-----------
9999

(The number of affected rows is 1)

*/

 

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.