• HowardW (6/18/2013)


    The Dixie Flatline (6/18/2013)


    You can do the conversions on the fly in a single query by using expressions in your joins and where clauses. It may be slower than joins using the same datatypes, but definitely faster than doing it by hand.

    declare @table1 table ( value1 varchar(50) )

    insert into @table1 select 'D19377322B06194DB6A9B34667D506A6'

    declare @table2 table ( value2 binary(50), value3 nvarchar(50))

    insert into @table2 select convert(binary,'0xD19377322B06194DB6A9B34667D506A6'), '{E867D837-EB0C-4EDB-B56E-D7083C181E52}'

    declare @table3 table ( value4 uniqueIdentifier)

    insert into @table3 select 'E867D837-EB0C-4EDB-B56E-D7083C181E52'

    select value1,value2,value3,value4

    from @table1 t1

    join @table2 t2 on t2.value2 = convert(binary,'0x'+t1.value1)

    join @table3 t3 on t3.value4 = substring(t2.value3,2, len(t2.value3)-2)

    Need to be careful doing conversions of Varchars into Binary.

    With the default style, you'll end up with a binary representation of the varchar, not the conversion of the Hex to Binary, which is what you actually want.

    Note the differences here:

    DECLARE @HexVarchar VARCHAR(32)='D19377322B06194DB6A9B34667D506A6'

    SELECT convert(VARBINARY,@HexVarchar)

    --0x443139333737333232423036313934444236413942333436363744353036

    SELECT convert(VARBINARY,@HexVarchar,2)

    --0xD19377322B06194DB6A9B34667D506A6

    I used this and I had tried the CONVERT function before but I was getting that 1st result with the CONVERT...Didnt know or think about putting a style in there! But once I did that, worked to perfection, thanks guys!