String Length

  • Comments posted to this topic are about the item String Length

    Kenneth FisherI was once offered a wizards hat but it got in the way of my dunce cap.--------------------------------------------------------------------------------For better, quicker answers on T-SQL questions, click on the following... http://www.sqlservercentral.com/articles/Best+Practices/61537/[/url]For better answers on performance questions, click on the following... http://www.sqlservercentral.com/articles/SQLServerCentral/66909/[/url]Link to my Blog Post --> www.SQLStudies.com[/url]

  • Nice Question 🙂

    [font="Verdana"] There is no Wrong time to do a Right thing 🙂 [/font]

  • Very good question.Refreshed the basics.

    I was not sure about middle two but sure about first two(7,7) and last two(14,80).

    So objective approach which I developed from Engineering Entrance Exam worked and got the right answer 😉

    --rhythmk
    ------------------------------------------------------------------
    To post your question use below link

    https://www.sqlservercentral.com/articles/forum-etiquette-how-to-post-datacode-on-a-forum-to-get-the-best-help
    🙂

  • Nice one , thanks

    Iulian

  • Great question, thanks!

    Need an answer? No, you need a question
    My blog at https://sqlkover.com.
    MCSE Business Intelligence - Microsoft Data Platform MVP

  • I thought the 4th one would be 8 at first, but since 7, 7, 8, 8, 14, 80 wasn't an option had to go by a process of elimination to find the right answer! Slightly odd behaviour from nchar() there, I learned something...

  • This was removed by the editor as SPAM

  • Good question - thanks

    -------------------------------Posting Data Etiquette - Jeff Moden [/url]Smart way to ask a question
    There are naive questions, tedious questions, ill-phrased questions, questions put after inadequate self-criticism. But every question is a cry to understand (the world). There is no such thing as a dumb question. ― Carl Sagan
    I would never join a club that would allow me as a member - Groucho Marx

  • Nice & Easy

    Thanks

  • 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/

  • Hugo Kornelis (7/11/2012)

    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.

    Well, it's strange behaviour of LEN() when you provide it with an NCHAR parameter, which is what I sort of meant but failed to express in any comprehensible manner... 🙂

  • Great question. Tests some basics that sometimes trips us up.

  • Nice one. I learned some new stuff today. 🙂

  • Why does datalength of nchar(40) give 80?!?? That really threw me.

    Thanks!

  • IowaDave (7/11/2012)


    Why does datalength of nchar(40) give 80?!?? That really threw me.

    Thanks!

    Because the "n" means the string is Unicode, so it has two bytes per character; DATALENGTH returns the actual length of the storage used for the field. 2 bytes per character * 40 characters = 80 bytes.

Viewing 15 posts - 1 through 15 (of 42 total)

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