It's probably more efficient express the value directly using dynamic sql (like your solution) but here is a function to convert a string representation of a varbinary value to varbinary. It uses one of MVJ's functions http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=47685if object_id('dbo.fn_strToVarBinary') > 0 drop function dbo.fn_strToVarBinarygocreate function dbo.fn_strToVarBinary (@s varchar(8000))returns varbinary(4000)asbegin declare @l int ,@vb varbinary(4000) select @l = len(@s) ,@vb = 0x select @vb = @vb+b from ( select number ,b = convert(binary(1), (convert(varbinary, charindex(substring(@s, number, 1), '0123456789ABCDEF')-1) * 16) + isNull(nullif(charindex(substring(@s, number+1, 1), '0123456789ABCDEF')-1,-1),0) ) from dbo.F_TABLE_NUMBER_RANGE(1,@l) where number%2 = 1 ) a order by number return substring(@vb,2,4000)endgoselect dbo.fn_strToVarBinary(sHex)from ( select '0x000000002FDAF785' sHex union all select '0x0A' union all select '0x160A' union all select '' union all select '0xA' --union all select replicate('FF', 3999) + 'AA' ) aoutput:-------------------------0x000000002FDAF7850x0A0x160A0x0xA0
Be One with the OptimizerTG