July 11, 2012 at 6:27 am
IowaDave (7/11/2012)
Why does datalength of nchar(40) give 80?!?? That really threw me.Thanks!
Because it is the entire length of the string * 2 bytes. Nchar includes padding so the length is 40 versus 7 with nvarchar which does not include padding.
July 11, 2012 at 6:57 am
Thanks for the great question. I was actually brushing up on len() and datalength() just a couple of days ago so it was still really fresh on my brain. 😀
July 11, 2012 at 7:02 am
Great question Kenneth, refreshed the old memory banks there 😉
_____________________________________________________________________
[font="Comic Sans MS"]"The difficult tasks we do immediately, the impossible takes a little longer"[/font]
July 11, 2012 at 7:20 am
thanks for the question.
Jason...AKA CirqueDeSQLeil
_______________________________________________
I have given a name to my pain...MCM SQL Server, MVP
SQL RNNR
Posting Performance Based Questions - Gail Shaw[/url]
Learn Extended Events
July 11, 2012 at 7:20 am
Good question!
July 11, 2012 at 7:22 am
Thanks Kenneth for a very good question. It was well written and explained. (Only improvement to code would be to alias computed columns.) And, with a short amount of code, several basic principles were highlighted. Well done!
[font="Verdana"]Please don't go. The drones need you. They look up to you.[/font]
Connect to me on LinkedIn
July 11, 2012 at 8:38 am
Thomas Abraham (7/11/2012)
Thanks Kenneth for a very good question. It was well written and explained. (Only improvement to code would be to alias computed columns.) And, with a short amount of code, several basic principles were highlighted. Well done!
I probably should have added alias' and usually would in code I write for someone else. In this case I wasn't really writing the code to be run so the alias' seemed a bit irrelevant.
Thanks everyone for the kind words. I was actually explaining the problem with using LEN on a CHAR to one of my coworkers when I decided it would make a good question. I've always liked the trick of adding a ; or some other character to the end of the string before using LEN. That and DATALENGTH is just a cool function.
Kenneth
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]
July 11, 2012 at 8:43 am
July 11, 2012 at 9:40 am
great question - cheers
July 11, 2012 at 10:23 am
Nice question!
It really bugs me that len() trims whitespace padding on the right.
July 11, 2012 at 2:22 pm
An interesting one -- thanks!
July 11, 2012 at 6:53 pm
I guess if you are guessing, its hard. Sort of like
2+2=
A) 3
B) 4
C) 5
D) 7
It all depends on whether or not you know what "+" means, which we all know from reading our Wittgenstein is a rather thorny philosphical issue.
July 16, 2012 at 5:09 pm
Excellent. I know what + means and still was challenged.
Not all gray hairs are Dinosaurs!
July 25, 2012 at 7:09 pm
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. 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().
I don't understand that, Hugo. LEN isn't doing anything with padding except ignoring it. It's the concatenate operation that turns the padding from padding to not padding by putting non-space characters beyond the padding.
Tom
July 26, 2012 at 11:41 pm
L' Eomot Inversé (7/25/2012)
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. 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().I don't understand that, Hugo. LEN isn't doing anything with padding except ignoring it. It's the concatenate operation that turns the padding from padding to not padding by putting non-space characters beyond the padding.
+1 I'm not sure what Hugo meant here either, but I agree with you Tom.
Viewing 15 posts - 16 through 30 (of 42 total)
You must be logged in to reply to this topic. Login to reply