SQL decimal and hexadecimal conversions

Source: Internet
Author: User

CREATE FUNCTIONUfn_convertint2hex (@Num BIGINT )RETURNS VARCHAR( -) asBEGIN/**************************************--function: Decimal to hex--garsonzhang--time: May 28, 2016 13:26:55--test: PRINT dbo.ufn_ Convertint2hex (*************************************)*/DECLARE @Result VARCHAR( -)SET @Result = "' while(@Num > 0 )BEGINSET @Result = SUBSTRING('0123456789ABCDEF',@Num %  - + 1,1)+ @ResultSET @Num = @Num /  -ENDRETURN @ResultENDGOCREATE FUNCTIONUfn_converthex2int (@HexString VARCHAR( -) )RETURNS BIGINT as /**************************************--function: Hexadecimal to decimal--garsonzhang--time: May 28, 2016 13:30:24--test: PRINT dbo.ufn_ Convertint2hex (*************************************)*/BEGIN DECLARE @Result BIGINTDECLARE @i INT ,@len INTDECLARE @power BIGINTSET @power =  -SELECT @i = 0 ,@Result = 0 ,@HexString = RTRIM(LTRIM(UPPER(@HexString)))SET @len = LEN(@HexString) IF(@len =  - )BEGIN IF(ASCII(SUBSTRING(@HexString,1,1))>  - )BEGIN --RaisError (' Out of data range ', 1, +)RETURN @ResultEND END -------------------------------------------------------  while(@i < @len )BEGIN IF( (SUBSTRING(@HexString,@len - @i,1) not between '0'  and '9' ) and(SUBSTRING(@HexString,@len - @i,1) not between 'A'  and 'F' ))BEGIN SET @Result = 0 Break;END ---------------------------------------- SET @Result = @Result +(CHARINDEX(SUBSTRING(@HexString,@len - @i,1),'0123456789ABCDEF')- 1 )* CAST(POWER(@power,@i) as BIGINT)SET @i = @i + 1 END ---------------------------------------------- RETURN @Result END

SQL decimal and hexadecimal conversions

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.