February 25, 2015 at 8:19 pm
Comments posted to this topic are about the item The answer to life, the universe and prime numbers
February 26, 2015 at 1:03 am
Nice question Eirikur. That was one of those where I knew the answer as soon as I saw the title but I still had to check because my Sci-Fi knowledge is much broader than my SQL Server knowledge and I didn't want to get cocky π
How to post a question to get the most help http://www.sqlservercentral.com/articles/Best+Practices/61537
February 26, 2015 at 2:50 am
The explanation is wrong, it's nothing to do with 'unassigned characters' (whatever they are).
The variable is declared as VARCHAR, which defaults to varchar(1). The calculation results in -51, which is too long to fit into this variable, so it puts in a '*'.
February 26, 2015 at 3:18 am
Not sure about this, had to run the code, never encountered such case before, so it is interesting question, thanx.
Thanks & Best Regards,
Hany Helmy
SQL Server Database Consultant
February 26, 2015 at 3:40 am
So Experts, which is the correct explanation. Either given by Eirikur Eiriksson or Toreador.
February 26, 2015 at 3:58 am
Try it out. You'll get the same result if you have
DECLARE @ VARCHAR = 10
or the following will return 45 (the ascii for '-')
DECLARE @ VARCHAR(3) = 1952 + 03 + 11 - 2001 - 05 - 11;
SELECT ASCII(@)
February 26, 2015 at 4:03 am
I'm no expert by any stretch of the imagination, but doing a bit of playing I have to agree with Toreador. If you change the code to this:
DECLARE @ VARCHAR(3) = 1952 + 03 + 11 - 2001 - 05 - 11;
SELECT ASCII(@) AS [the answer to life, the universe and prime numbers];
you get an answer of 45 which is the ascii code for "-".
February 26, 2015 at 4:10 am
In the explanation I used the term unassigned for the lack of a better one as the value of the variable is neither truncated nor is it null.
π
February 26, 2015 at 4:14 am
hmmmm .... it can also be named as Theory of Everything π
nice question and thanks for sharing Eirikur.
February 26, 2015 at 4:54 am
This was removed by the editor as SPAM
February 26, 2015 at 5:28 am
Toreador (2/26/2015)
The explanation is wrong, it's nothing to do with 'unassigned characters' (whatever they are).The variable is declared as VARCHAR, which defaults to varchar(1). The calculation results in -51, which is too long to fit into this variable, so it puts in a '*'.
Thanks, I had thought VARCHAR defaulted to 50 and could not figure why the answer was not 45.
February 26, 2015 at 5:43 am
djj (2/26/2015)
Toreador (2/26/2015)
The explanation is wrong, it's nothing to do with 'unassigned characters' (whatever they are).The variable is declared as VARCHAR, which defaults to varchar(1). The calculation results in -51, which is too long to fit into this variable, so it puts in a '*'.
Thanks, I had thought VARCHAR defaulted to 50 and could not figure why the answer was not 45.
When declared as a variable, I believe the default length is still 1. When used in a CONVERT function, it's 30.
February 26, 2015 at 5:45 am
This was removed by the editor as SPAM
February 26, 2015 at 5:45 am
Oh yeah...nice question, Erikur. I knew the answer without doing any math before even looking at the answers, but I still checked out the code to make sure. π
February 26, 2015 at 6:42 am
This was removed by the editor as SPAM
Viewing 15 posts - 1 through 15 (of 36 total)
You must be logged in to reply to this topic. Login to reply