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