﻿<?xml version='1.0' encoding='UTF-8'?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/"><channel><title>SQLServerCentral / Article Discussions / Article Discussions by Author / Discuss content posted by bitbucket  / Arithmetic 1 / Latest Posts</title><generator>InstantForum.NET v2.9.0</generator><description>SQLServerCentral</description><link>http://www.sqlservercentral.com/Forums/</link><webMaster>notifications@sqlservercentral.com</webMaster><lastBuildDate>Wed, 19 Jun 2013 08:00:32 GMT</lastBuildDate><ttl>20</ttl><item><title>RE: Arithmetic 1</title><link>http://www.sqlservercentral.com/Forums/Topic1259233-1222-1.aspx</link><description>nice question.....Manikandanps------Go fast as possible---------------</description><pubDate>Tue, 29 Jan 2013 04:00:55 GMT</pubDate><dc:creator>manik123</dc:creator></item><item><title>RE: Arithmetic 1</title><link>http://www.sqlservercentral.com/Forums/Topic1259233-1222-1.aspx</link><description>Good question. Thanks for submitting.</description><pubDate>Wed, 14 Mar 2012 20:01:59 GMT</pubDate><dc:creator>Britt Cluff</dc:creator></item><item><title>RE: Arithmetic 1</title><link>http://www.sqlservercentral.com/Forums/Topic1259233-1222-1.aspx</link><description>For any numeric operation to be successful,  atleast one of the operand must be a number data type.e.g. select '6'+'4'*'8'/'2' will fail where as select '6'+4*'8'/'2' will succeed.Thus it is obvious that the execution expression 2 fails.</description><pubDate>Sun, 11 Mar 2012 08:46:41 GMT</pubDate><dc:creator>muni.reddy</dc:creator></item><item><title>RE: Arithmetic 1</title><link>http://www.sqlservercentral.com/Forums/Topic1259233-1222-1.aspx</link><description>Interesting question, thanks.</description><pubDate>Mon, 05 Mar 2012 00:36:40 GMT</pubDate><dc:creator>Koen Verbeeck</dc:creator></item><item><title>RE: Arithmetic 1</title><link>http://www.sqlservercentral.com/Forums/Topic1259233-1222-1.aspx</link><description>Nice question. Thanks.</description><pubDate>Thu, 01 Mar 2012 21:46:47 GMT</pubDate><dc:creator>Hardy21</dc:creator></item><item><title>RE: Arithmetic 1</title><link>http://www.sqlservercentral.com/Forums/Topic1259233-1222-1.aspx</link><description>Nice question, thanks!</description><pubDate>Thu, 01 Mar 2012 09:20:15 GMT</pubDate><dc:creator>KWymore</dc:creator></item><item><title>RE: Arithmetic 1</title><link>http://www.sqlservercentral.com/Forums/Topic1259233-1222-1.aspx</link><description>tks for the question</description><pubDate>Wed, 29 Feb 2012 17:29:52 GMT</pubDate><dc:creator>OzYbOi d(-_-)b</dc:creator></item><item><title>RE: Arithmetic 1</title><link>http://www.sqlservercentral.com/Forums/Topic1259233-1222-1.aspx</link><description>Hi All,I tried to make a conclusion based on the data type precedence, but did not manage.If you see the data type precedencehttp://msdn.microsoft.com/en-us/library/ms190276.aspx, it is also ambiguous for "%" showing different behavior against some cases for "*" and "/".* (Multiply), / (Division), % (Modulo) belong to level 2, while the others are in lower levels and for which there are no errors.In any case, interesting example!</description><pubDate>Wed, 29 Feb 2012 16:35:51 GMT</pubDate><dc:creator>IgorMi</dc:creator></item><item><title>RE: Arithmetic 1</title><link>http://www.sqlservercentral.com/Forums/Topic1259233-1222-1.aspx</link><description>Very nice, Ron - thanks!</description><pubDate>Wed, 29 Feb 2012 14:31:00 GMT</pubDate><dc:creator>Revenant</dc:creator></item><item><title>RE: Arithmetic 1</title><link>http://www.sqlservercentral.com/Forums/Topic1259233-1222-1.aspx</link><description>Nice question that I think most of have run into at some point by accident. Thanks.</description><pubDate>Wed, 29 Feb 2012 10:43:33 GMT</pubDate><dc:creator>DBA_Dom</dc:creator></item><item><title>RE: Arithmetic 1</title><link>http://www.sqlservercentral.com/Forums/Topic1259233-1222-1.aspx</link><description>Rock-solid question thanks to Ron and his staff of reviewers!</description><pubDate>Wed, 29 Feb 2012 10:21:29 GMT</pubDate><dc:creator>Rose Bud</dc:creator></item><item><title>RE: Arithmetic 1</title><link>http://www.sqlservercentral.com/Forums/Topic1259233-1222-1.aspx</link><description>Nice question.I think the reason for the error in the second batch is that in order to do implicit conversion for arguments of an operand the compiler must know what type is required (maybe this is what Hugo was trying to say); this is true for all operators, in particular for all arithmetic operators (+,-,*,/,%) with only the + case failing to produce an error when both arguments have string types, and that only because the '+' symbol is overloaded with sting concatenation as well as with the various numeric variants of addition (unrounded, or with various different kinds of rounding).POWER of course is different: in T-SQL it isn't an operator (it's a function), so the same rules don't apply.  The first argument is explicitly required to be float (ie float(53)) or something that can be explicitly converted to it, so there's no issue with implicit conversion for that.  The second parameter breaks the rules that would apply if POWER were an operator, because in theory compiler doesn't know what it has to be converted to (it can have any exact or approximate numeric type except bit, or any type implicitly convertible to an exact or approximate numeric), but I suspect that in practise it is converted to float (it is probably documented somewhere, but I've no idea where) and if my suspicion is justified the specification could have been written the same way for the second parameter as for the first and we would then be able to have an operator-style syntax for it (** or ^ or ↑ as in many languages) while maintaining the implicit conversion rules, instead of having to use function syntax for this arithmetic operator.</description><pubDate>Wed, 29 Feb 2012 09:25:54 GMT</pubDate><dc:creator>L' Eomot Inversé</dc:creator></item><item><title>RE: Arithmetic 1</title><link>http://www.sqlservercentral.com/Forums/Topic1259233-1222-1.aspx</link><description>Good question, but I feel compelled to point out that all statements would fail on a Case Sensitive Collation because the variable @b is declared, but @B is referenced. Also in example #5 @A is declared and @a is referenced.In my opinion as rare as it may be to be in a case sensitive database, one should always be consistent in coding.**** I'm not trying to bust your chops, just making a point. :-D</description><pubDate>Wed, 29 Feb 2012 08:56:24 GMT</pubDate><dc:creator>Jason Selburg</dc:creator></item><item><title>RE: Arithmetic 1</title><link>http://www.sqlservercentral.com/Forums/Topic1259233-1222-1.aspx</link><description>Thanks Ron</description><pubDate>Wed, 29 Feb 2012 08:53:30 GMT</pubDate><dc:creator>SQLRNNR</dc:creator></item><item><title>RE: Arithmetic 1</title><link>http://www.sqlservercentral.com/Forums/Topic1259233-1222-1.aspx</link><description>Thank you for the question!!I agree with all others that having at least one numeric causes implicit conversion for the char values, but 2 chars cannot be multiplied.Yes, we have to be very careful with implicit conversion.I remember in VAX VMS BASIC you could do operations with numbers as chars, and they had a much higher precision that numeric data types (e.g. I was able to calculate PI with over 100 decimals), that was long ago, and I haven't seen any other language have this feature."El" Jerry.</description><pubDate>Wed, 29 Feb 2012 07:37:28 GMT</pubDate><dc:creator>EL Jerry</dc:creator></item><item><title>RE: Arithmetic 1</title><link>http://www.sqlservercentral.com/Forums/Topic1259233-1222-1.aspx</link><description>[quote]I could not locate a valid explanation[/quote]And hyet, the explanation is simple. I lack the time to find the proper Books Online pages at this time, but they are in fact easy to find.In five out of six batches, one operand is a character value and the other is an integer value. The rules of data type precedence specify (look up: "Data type precedence" in books online) that in such a case, the character operand (lower precedence) is converted to int (higher precedence) before the operator is invoked. The operator then operates on two integer values.In the batch that errors, both operands are string. No data type conversion is required, and the operator is invoked. If the operator would have been +, that would have resulted in string concatenation. But the operator * is undefined for string operands. So, no bugs, no incorrect answer; SQL Server is operating is intended and as documented.Overall a good question, but a disappointing explanation. Also, the subtle differences in the form of the batches made it harder to see what was going on. I almost jumped to the conclusion that all batches combined an int and a char value; the only reason that I continued to lkook deeper was that I figured that there had to be a catch somewhere. With u uniform format for all the questions, it would have been easier to spot the differences and focus on the skill the question indended to test.</description><pubDate>Wed, 29 Feb 2012 07:29:18 GMT</pubDate><dc:creator>Hugo Kornelis</dc:creator></item><item><title>RE: Arithmetic 1</title><link>http://www.sqlservercentral.com/Forums/Topic1259233-1222-1.aspx</link><description>This script demonstrates some problem with implicit conversion:[code="sql"]declare @v varchar(30),@f float    -- here you think it is all right and it is    set @f = 1.12    set @v = @f    select @v        -- here you think it is all right, but you get the wrong result    set @f = 123456.12    set @v = @f    select @v            -- worst and worst    set @f = 12345677.12    set @v = @f    select @v                -- correct solution    set @f = 12345677.12    set @v = CAST(@f AS DECIMAL(16,2))    select @v        [/code]</description><pubDate>Wed, 29 Feb 2012 06:39:30 GMT</pubDate><dc:creator>Carlo Romagnano</dc:creator></item><item><title>RE: Arithmetic 1</title><link>http://www.sqlservercentral.com/Forums/Topic1259233-1222-1.aspx</link><description>[quote][b]jcb (2/29/2012)[/b][hr]Yes! Implicit convertion is evil.Even worst when converting dates and using fancy collations and no US-en.[/quote]Fancy collations :-P  Tomorow morning in Bois de Boulogne :-D</description><pubDate>Wed, 29 Feb 2012 06:03:23 GMT</pubDate><dc:creator>honza.mf</dc:creator></item><item><title>RE: Arithmetic 1</title><link>http://www.sqlservercentral.com/Forums/Topic1259233-1222-1.aspx</link><description>Excellent question. Code parsing for a reason, with clear explanation. Thanks bitbucket!</description><pubDate>Wed, 29 Feb 2012 05:31:26 GMT</pubDate><dc:creator>Thomas Abraham</dc:creator></item><item><title>RE: Arithmetic 1</title><link>http://www.sqlservercentral.com/Forums/Topic1259233-1222-1.aspx</link><description>good question!!!thanks Ron!!!</description><pubDate>Wed, 29 Feb 2012 04:28:43 GMT</pubDate><dc:creator>rfr.ferrari</dc:creator></item><item><title>RE: Arithmetic 1</title><link>http://www.sqlservercentral.com/Forums/Topic1259233-1222-1.aspx</link><description>I'm pretty sure SQL ill not multiply chars '1' * '4' it demands at least one numeric type to decide for the result type.Yes! Implicit convertion is evil.Even worst when converting dates and using fancy collations and no US-en.</description><pubDate>Wed, 29 Feb 2012 04:17:36 GMT</pubDate><dc:creator>jcb</dc:creator></item><item><title>RE: Arithmetic 1</title><link>http://www.sqlservercentral.com/Forums/Topic1259233-1222-1.aspx</link><description>I would thing the devil were in the details.In fact a lot of the questions would fail if not for the fact that the numeric in the question is in fact a numeric.The trap, if we can call it that, is that no none-character values were there to coax the cast. If #2 had been constructed like the others, thus having the number '4' as a number in stead of a string, the statement would have done the cast and no error would have followed.Inserting a character i e.g. #4 would of course cause that one to fail as well.</description><pubDate>Wed, 29 Feb 2012 03:59:35 GMT</pubDate><dc:creator>Keld Laursen (VFL)</dc:creator></item><item><title>RE: Arithmetic 1</title><link>http://www.sqlservercentral.com/Forums/Topic1259233-1222-1.aspx</link><description>[quote][b]Raghavendra Mudugal (2/29/2012)[/b][hr]It gives me this error on the 2nd blockMsg 8117, Level 16, State 1, Line 3Operand data type char is invalid for multiply operator.I guess E stands for ERROR?[/quote]You wouldn't have had to guess if you read the statement after the SQL:[quote]For simplity in creating the possible answers each answer is composed of a comma separated list [b]and if execution of one of the above generates an error the letter E will be included in the list.[/b] [/quote]</description><pubDate>Wed, 29 Feb 2012 03:24:18 GMT</pubDate><dc:creator>cengland0</dc:creator></item><item><title>RE: Arithmetic 1</title><link>http://www.sqlservercentral.com/Forums/Topic1259233-1222-1.aspx</link><description>[quote][b]Carlo Romagnano (2/29/2012)[/b][hr]This kind of error also is independent from version of sqlserver.You can not use - * / operators with chars[/quote]Well, you obviously can - as long as one of the other operators is a numeric type, so that SQL can decide what to attempt to convert the chars to. That's the level of guidance that SQL expects and it seems a decent compromise, rather than just making it so it will attempt a conversion to a non-specified type.</description><pubDate>Wed, 29 Feb 2012 03:09:12 GMT</pubDate><dc:creator>call.copse</dc:creator></item><item><title>RE: Arithmetic 1</title><link>http://www.sqlservercentral.com/Forums/Topic1259233-1222-1.aspx</link><description>Yet another question that tells us not to rely on implicit conversion...</description><pubDate>Wed, 29 Feb 2012 02:34:59 GMT</pubDate><dc:creator>Toreador</dc:creator></item><item><title>RE: Arithmetic 1</title><link>http://www.sqlservercentral.com/Forums/Topic1259233-1222-1.aspx</link><description>This kind of error also is independent from version of sqlserver.You can not use - * / operators with chars</description><pubDate>Wed, 29 Feb 2012 01:54:41 GMT</pubDate><dc:creator>Carlo Romagnano</dc:creator></item><item><title>RE: Arithmetic 1</title><link>http://www.sqlservercentral.com/Forums/Topic1259233-1222-1.aspx</link><description>It gives me this error on the 2nd blockMsg 8117, Level 16, State 1, Line 3Operand data type char is invalid for multiply operator.I guess E stands for ERROR?</description><pubDate>Wed, 29 Feb 2012 01:48:01 GMT</pubDate><dc:creator>Raghavendra Mudugal</dc:creator></item><item><title>RE: Arithmetic 1</title><link>http://www.sqlservercentral.com/Forums/Topic1259233-1222-1.aspx</link><description>I particularly like the fact that a QOTD was verified by several people before publication.</description><pubDate>Wed, 29 Feb 2012 01:45:20 GMT</pubDate><dc:creator>BrainDonor</dc:creator></item><item><title>RE: Arithmetic 1</title><link>http://www.sqlservercentral.com/Forums/Topic1259233-1222-1.aspx</link><description>[quote][b]raulggonzalez (2/29/2012)[/b][hr]The explanation I see is that on answers 1,3,4,5,6 implicit data types conversion happens because of mixing INT and CHAR types.On the answer 2, we just try to multiply 2 CHAR, therefore an error is raised up.Good question![/quote]Yeah, I have to agree, multiplying strings is the cause. It errors before it even tries to implicitly cast the answer to an int.</description><pubDate>Wed, 29 Feb 2012 01:41:55 GMT</pubDate><dc:creator>Christy M</dc:creator></item><item><title>RE: Arithmetic 1</title><link>http://www.sqlservercentral.com/Forums/Topic1259233-1222-1.aspx</link><description>The explanation I see is that on answers 1,3,4,5,6 implicit data types conversion happens because of mixing INT and CHAR types.On the answer 2, we just try to multiply 2 CHAR, therefore an error is raised up.Good question!</description><pubDate>Wed, 29 Feb 2012 01:37:54 GMT</pubDate><dc:creator>raulggonzalez</dc:creator></item><item><title>RE: Arithmetic 1</title><link>http://www.sqlservercentral.com/Forums/Topic1259233-1222-1.aspx</link><description>Very interesting question. Learnt something new again.</description><pubDate>Wed, 29 Feb 2012 00:38:55 GMT</pubDate><dc:creator>Michael Riemer</dc:creator></item><item><title>RE: Arithmetic 1</title><link>http://www.sqlservercentral.com/Forums/Topic1259233-1222-1.aspx</link><description>Good Question....</description><pubDate>Tue, 28 Feb 2012 22:10:59 GMT</pubDate><dc:creator>Ramana Reddy P</dc:creator></item><item><title>RE: Arithmetic 1</title><link>http://www.sqlservercentral.com/Forums/Topic1259233-1222-1.aspx</link><description>Good Question. Back to the basics. +1 to the total.</description><pubDate>Tue, 28 Feb 2012 20:43:11 GMT</pubDate><dc:creator>baabhu</dc:creator></item><item><title>Arithmetic 1</title><link>http://www.sqlservercentral.com/Forums/Topic1259233-1222-1.aspx</link><description>Comments posted to this topic are about the item [B]&lt;A HREF="/questions/Arithmetic/88519/"&gt;Arithmetic 1&lt;/A&gt;[/B]</description><pubDate>Tue, 28 Feb 2012 20:41:26 GMT</pubDate><dc:creator>bitbucket-25253</dc:creator></item></channel></rss>