简体   繁体   中英

Inserting/Removing hex padding with SQL Server 2008 R2?

I am working with a database that encodes strings to hex and then pads the entire string with 0 's.

Like this:

Origional String:           PartitionTest
JUST Hex-encoded Strring:   0x506172746974696f6e54657374
The output I see:           0x50006100720074006900740069006F006E005400650073007400

Is there anyway for me to remove the spaces and decode the final string back to text? AND, how would I then encode that text back to the padded string? I am already converting the string to hex with

SELECT 
    MASTER.dbo.fn_varbintohexstr(CAST('PartitionTest' AS VARBINARY))

but I don't have a clue how to pad that result out.

Thank you!

EDIT: As input is varbinary, need to first convert it into varchar to use STUFF.

Declare @input varbinary(128) = 0x50006100720074006900740069006F006E005400650073007400
Declare @Temp as VARCHAR(350)
Set @Temp = CONVERT(VARCHAR(350), @input ,2)
declare @length int
set @length = len(@Temp)
select @length
declare @i int = 3


WHILE @i < @length/2+2
BEGIN
    Set @Temp = Stuff(@Temp, @i, 2, '')
    set @i = @i +2

END

select '0x' + @Temp

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM