I have a variable that is a hex with reversed bytes. To reverse bytes I found the solution with type binary and reversing it depends on its size. Since I have to reverse bytes of variable @card which can be different sizes I was thinking of declaring variable @binarylen, type int, which I want to set as data type length but get an error 'incorrect syntax: expecting ID, INTEGER or QOUTED_ID'. Can I somehow do this?
DECLARE @card_hex nvarchar(max)
DECLARE @card_int bigint
DECLARE @badgenr nvarchar(max)
set @badgenr= '120224205061286445'
-- convert it to hex
set @card_int = convert (bigint, (@badgenr))
print @card_int
set @card_hex = CONVERT(NVARCHAR(31),CONVERT(VARBINARY, @card_int),2)
print @card_hex
--remove leading zeroes:
set @card_hex = (select substring(@card_hex, patindex('%[^0]%',@card_hex), 20))
print @card_hex
DECLARE @bitelen as int
set @bitelen= LEN(@card_hex)
declare @binarylen as int
set @binarylen=@bitelen/2 + @bitelen%2
declare @reverse as binary(@binarylen)
set @reverse=( SELECT cast(reverse(cast(@card_int AS BINARY(@binarylen))) AS BINARY(@binarylen)))