Convert decimal to hexadecimal

Source: Internet
Author: User
Create Function DBO. f_dec_hex (@ num bigint, @ length INT)
Returns varchar (16)
As
Begin
-- Author: pbsql
-- @ Num is the positive integer to be converted to a hexadecimal string. If it is a negative number, '0' is returned'
-- @ Length indicates the length of the hexadecimal string to be returned.
-- If @ length is negative, '0' is returned. If it is 0, a hexadecimal string of the actual length is returned.
-- If @ length is greater than 0, return the hexadecimal string with the length of @ length (from the low position, the BITs do not fill in '0' when the BITs do not exceed the bits, and the bits are truncated when the BITs do not exceed the bits)
Declare @ result varchar (16)
Set @ result =''
If @ num <= 0 or @ length <0
Set @ result = '0'
Else
Begin
While @ num <> 0
Select @ result = substring ('0123456789abcdef ', @ num % 16 + 1, 1) + @ result, @ num = @ num/16
If @ length> 0
Set @ result = right (replicate ('0', @ length) + @ result, @ length)
End
Return @ result
End
Go

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.