SQL SERVER Custom Function integer converts a specified length of 16 binary to a specified length of 16 out of 0

Source: Internet
Author: User

When the project was recently expanded, the problem was to convert the integral type to a specified length of 16 binary

At first it was the direct use of cast (varbinary (4)) but finding out this doesn't solve my problem.

So I searched the Internet, and then changed, the following is the function:

1 Create FunctionInttohexlength (@Num int,@HxLength int)2 returns varchar( -)3  as4 begin5   Declare @Mods int,@res varchar( -),@Length int6   Set @res="'7    while @Num <> 0 8   begin9     Set @Mods =@Num %  -Ten     if @Mods > 9  One       Set @res = Char(Ascii('A')+@Mods-Ten)+@res A     Else  -       Set @res = Cast(@Mods  as varchar(4))+ @res -     Set @Num = @Num/ - the   End -   Set @Length=@HxLength-datalength(@res) -   if(datalength(@res)<@HxLength) -   BEGIN +        while @Length<>0 -       begin +       SET @res='0'+@res A       Set @Length=@Length-1 at       End -   END -   return @res - End
View Code


Then use SELECT dbo. Inttohexlength (15,4)

To get: This will meet the requirements!

SQL SERVER Custom Function integer converts a specified length of 16 binary to a specified length of 16 out of 0

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.