• Evgeny - Monday, December 11, 2017 7:06 PM

    sknox - Monday, December 11, 2017 6:38 PM

    I'd like to continue testing this on different versions and with different settings and see if we can identify what variables lead to this not working. Could you run the test code in my post directly above yours and share the results? Thanks!

    Looks like majority of people got the same result:

    0x410042004300440045004600
    A

    The collation on my servers is Latin1_General_CI_AS, versions are 13.0.4435 and 13.0.1601. I think that code might work on a server with a binary collation, but right now I don't have one handy.

    No, my servers are SQL_Latin1_General_CP1_CI_AS as well. If I were to guess, I'd say it's a string component interpreting CHAR(0) as a string terminator rather than testing the length of the string. What confuses me is that behavior should not change that much between versions, which is why I'm looking for a configuration difference.
    The varbinary results show that the full 12 characters are still there. I wonder what you'd get with:

    declare @a varchar(6) = 'ABCDEF'
    --select cast(cast(@A as nvarchar(12)) as varbinary(12))
    select cast(cast(cast(cast(@A as nvarchar(12)) as varbinary(12)) as varchar(12)) as varbinary(12))

    That might show if the cast to varchar is truncating the results or not.