How to concatenate binary variable with string?

  • Hello, I am having a problem concatenating a binary variable to a string. A simplified example:

    DECLARE @mybin1 binary(8)

    SET @mybin1 = 0x0000000000045F7C

    PRINT CONVERT(varchar(8), @mybin1) + ' TEST'

    The result is:

    strange characters.... + ' TEST'

    In the end I'm using an EXEC ('SELECT...') statement within my code, and want to use the binary value in a WHERE clause for filtering purposes. For example:

    DECLARE @mybin1 varchar(8)

    SET @mybin1 = CONVERT(varchar(8),0x0000000000045F7C)

    EXEC ('SELECT * FROM TestTable WHERE TimeStamp > ' + @mybin1)

    With this I get the following error:

    Msg 102, Level 15, State 1, Line 1

    Incorrect syntax near '|'.

    If I don't try to convert/concatenate the binary variable to the string it works as in this example:

    DECLARE @mybin1 binary(8)

    SET @mybin1 = 0x0000000000045F7C

    SELECT * FROM TestTable WHERE TimeStamp > @mybin1

    I think I need to convert the binary variable to a literal string '0x0000000000045F7C' for this to work. How to do this?

    Thanks in advance!

  • DECLARE @mybin1 binary(8)

    SET @mybin1 = 0x0000000000045F7C

    PRINT CONVERT(varchar(8), @mybin1) + ' TEST'

    SELECTmaster.sys.fn_varbintohexstr(@mybin1) + ' TEST'


    N 56°04'39.16"
    E 12°55'05.25"

  • Thanks so much! That worked great!

  • Awsome.!!!! thanks..!!!!

Viewing 4 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic. Login to reply