SQL varbinary varchar Mutual transfer

Source: Internet
Author: User

ALTER function Varbin2hexstr (@bin varbinary (8000)) returns varchar (8000)     asBEGIN DECLARE @re varchar (8000), @iint        Select@re ="', @i=datalength (@bin) while@i>0            Select@re =substring ('0123456789ABCDEF', substring (@bin, @i,1)/ -+1,1)                    +substring ('0123456789ABCDEF', substring (@bin, @i,1)% -+1,1)                    +@re, @i[Email protected]1--return('0x'+@re)return@re End
ALTER function [dbo]. [Hexstr2varbin] (@char varchar (8000)) returns varbinary (8000) asBEGIN DECLARE @re varbinary (8000), @tempchar varchar (2), @getchar varchar (1), @getintint, @nint, @totalintint, @iint, @tempintint, @runNumint--number of string intercept segmentsSelect@tempchar ="', @i=datalength (@char), @re =0x; if(@i>0) Beginif(@i%2=0)Set@runNum = @i/2        Else Set@runNum = @i/2+1          while(@runNum >0) Beginif(@runNum =1)Set@tempchar =@charElse Set@tempchar = substring (@char, (@runNum-1)*2,2)            Select@n=1, @totalint =0; --Loop through each string that is intercepted (the string length here is 2) while@n < (datalength (@tempchar) +1) BeginSet@getchar =substring (@tempchar, @n,1); --converts a character to a hexadecimal corresponding numberSelect@getint = CaseWhen @getchar='a'ThenTenWhen @getchar='b'Then OneWhen @getchar='C'Then AWhen @getchar='D'Then -When @getchar='e'Then -When @getchar='F'Then the                       ElseConvertint, @getchar) end; Set@[email Protected]*power ( -, Datalength (@tempchar)-@n)Set@totalint = @totalint +@tempintSet@[email protected]+1EndSet@re =convert (varbinary (1), @totalint) +@re; Set@[email protected]1; End Endreturn@reend

SQL varbinary varchar Mutual transfer

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.