SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Arithmetic 1


Arithmetic 1

Author
Message
call.copse
call.copse
SSCrazy Eights
SSCrazy Eights (8.9K reputation)SSCrazy Eights (8.9K reputation)SSCrazy Eights (8.9K reputation)SSCrazy Eights (8.9K reputation)SSCrazy Eights (8.9K reputation)SSCrazy Eights (8.9K reputation)SSCrazy Eights (8.9K reputation)SSCrazy Eights (8.9K reputation)

Group: General Forum Members
Points: 8945 Visits: 2157
Carlo Romagnano (2/29/2012)
This kind of error also is independent from version of sqlserver.
You can not use - * / operators with chars


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.
cengland0
cengland0
Hall of Fame
Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)

Group: General Forum Members
Points: 3326 Visits: 1300
Raghavendra Mudugal (2/29/2012)
It gives me this error on the 2nd block

Msg 8117, Level 16, State 1, Line 3
Operand data type char is invalid for multiply operator.

I guess E stands for ERROR?


You wouldn't have had to guess if you read the statement after the SQL:
For simplity in creating the possible answers each answer is composed of a comma separated list and if execution of one of the above generates an error the letter E will be included in the list.

Keld Laursen (SEGES)
Keld Laursen (SEGES)
Mr or Mrs. 500
Mr or Mrs. 500 (579 reputation)Mr or Mrs. 500 (579 reputation)Mr or Mrs. 500 (579 reputation)Mr or Mrs. 500 (579 reputation)Mr or Mrs. 500 (579 reputation)Mr or Mrs. 500 (579 reputation)Mr or Mrs. 500 (579 reputation)Mr or Mrs. 500 (579 reputation)

Group: General Forum Members
Points: 579 Visits: 214
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.
jcb
jcb
SSCarpal Tunnel
SSCarpal Tunnel (4.7K reputation)SSCarpal Tunnel (4.7K reputation)SSCarpal Tunnel (4.7K reputation)SSCarpal Tunnel (4.7K reputation)SSCarpal Tunnel (4.7K reputation)SSCarpal Tunnel (4.7K reputation)SSCarpal Tunnel (4.7K reputation)SSCarpal Tunnel (4.7K reputation)

Group: General Forum Members
Points: 4726 Visits: 1044
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.
rfr.ferrari
rfr.ferrari
Hall of Fame
Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)

Group: General Forum Members
Points: 3743 Visits: 13648
good question!!!
thanks Ron!!!


rfr.ferrari
DBA - SQL Server 2008
MCITP | MCTS

remember is live or suffer twice!
the period you fastest growing is the most difficult period of your life!

Thomas Abraham
Thomas Abraham
SSCertifiable
SSCertifiable (6.1K reputation)SSCertifiable (6.1K reputation)SSCertifiable (6.1K reputation)SSCertifiable (6.1K reputation)SSCertifiable (6.1K reputation)SSCertifiable (6.1K reputation)SSCertifiable (6.1K reputation)SSCertifiable (6.1K reputation)

Group: General Forum Members
Points: 6071 Visits: 2256
Excellent question. Code parsing for a reason, with clear explanation. Thanks bitbucket!

Please don't go. The drones need you. They look up to you.
Connect to me on LinkedIn
honza.mf
honza.mf
Hall of Fame
Hall of Fame (3.6K reputation)Hall of Fame (3.6K reputation)Hall of Fame (3.6K reputation)Hall of Fame (3.6K reputation)Hall of Fame (3.6K reputation)Hall of Fame (3.6K reputation)Hall of Fame (3.6K reputation)Hall of Fame (3.6K reputation)

Group: General Forum Members
Points: 3609 Visits: 1323
jcb (2/29/2012)
Yes! Implicit convertion is evil.
Even worst when converting dates and using fancy collations and no US-en.

Fancy collations :-P Tomorow morning in Bois de Boulogne :-D



See, understand, learn, try, use efficient
© Dr.Plch
Carlo Romagnano
Carlo Romagnano
SSChampion
SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)

Group: General Forum Members
Points: 12414 Visits: 3517
This script demonstrates some problem with implicit conversion:
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


Hugo Kornelis
Hugo Kornelis
SSC-Dedicated
SSC-Dedicated (34K reputation)SSC-Dedicated (34K reputation)SSC-Dedicated (34K reputation)SSC-Dedicated (34K reputation)SSC-Dedicated (34K reputation)SSC-Dedicated (34K reputation)SSC-Dedicated (34K reputation)SSC-Dedicated (34K reputation)

Group: General Forum Members
Points: 34572 Visits: 13121
I could not locate a valid explanation

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.


Hugo Kornelis, SQL Server/Data Platform MVP (2006-2016)
Visit my SQL Server blog: http://sqlblog.com/blogs/hugo_kornelis
EL Jerry
EL Jerry
SSCertifiable
SSCertifiable (5.1K reputation)SSCertifiable (5.1K reputation)SSCertifiable (5.1K reputation)SSCertifiable (5.1K reputation)SSCertifiable (5.1K reputation)SSCertifiable (5.1K reputation)SSCertifiable (5.1K reputation)SSCertifiable (5.1K reputation)

Group: General Forum Members
Points: 5109 Visits: 1346
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.

"El" Jerry.

"A watt of Ottawa" - Gerardo Galvan

To better understand your help request, please follow these best practices.
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum








































































































































































SQLServerCentral


Search