• paul.knibbs (7/11/2012)


    Slightly odd behaviour from nchar() there, I learned something...

    Hmmm, not sure why you this is strange behaviour for nchar. It is fixed length, so any shorter length string put into it has to be padded. And if you then concatenate another string, it will of course go after the original string (which includes the padding). If you don't want the padding to be in there, use LTRIM() (or use nvarchar instead of nchar, obviously).

    In my opinion, it is LEN() that has the strange behaviour here, because it returns the length of the padded version of the string instead of the length of the string itself. This almost caused me to give the wrong answer - but I recalled just in time the number of times I have already been bitten by this strange behaviour of LEN().

    Thanks for a good question, Kenneth!


    Hugo Kornelis, SQL Server/Data Platform MVP (2006-2016)
    Visit my SQL Server blog: https://sqlserverfast.com/blog/
    SQL Server Execution Plan Reference: https://sqlserverfast.com/epr/