April 14, 2005 at 4:21 am
Hi ya,
consider the following script
SET NOCOUNT ON
CREATE TABLE testcase
(
MyDecimal DECIMAL(2, 0) NOT NULL
)
CREATE INDEX ix_test ON testcase (MyDecimal)
INSERT INTO testcase VALUES (2)
SELECT * FROM testcase WITH (INDEX=0) WHERE MyDecimal <999999
--SELECT * FROM testcase WHERE MyDecimal <999999
SELECT * FROM testcase WHERE CAST(MyDecimal AS DECIMAL(6,0)) <999999
DROP TABLE testcase
SET NOCOUNT OFF
MyDecimal
---------
2
MyDecimal
---------
2
This works just fine. However, when you uncomment the second SELECT statement, you'll receive a
Server: Msg 8115, Level 16, State 8, Line 1
Arithmetic overflow error converting numeric to data type numeric.
I'm on Microsoft SQL Server 2000 - 8.00.760. The original questioner claims that this behaviour came with SP3/3a. Running this against an unpatched server should work.
Since I don't have an unpatched server available, can someone please verify or falsify this for me?
--
Frank Kalis
Microsoft SQL Server MVP
Webmaster: http://www.insidesql.org/blogs
My blog: http://www.insidesql.org/blogs/frankkalis/[/url]
April 14, 2005 at 4:54 am
Works fine without error after the select comment is removed on an unpatched server.
April 14, 2005 at 5:04 am
Try this,
SET NOCOUNT ON
CREATE TABLE testcase
(
MyDecimal DECIMAL(2, 0) NOT NULL
)
CREATE INDEX ix_test ON testcase (MyDecimal)
DECLARE @v-2 REAL
SET @v-2=999999
INSERT INTO testcase VALUES (2)
SELECT * FROM testcase WITH (INDEX=0) WHERE MyDecimal <999999
SELECT * FROM testcase WHERE MyDecimal < @v-2 -- 999999
SELECT * FROM testcase WHERE CAST(MyDecimal AS DECIMAL(6,0)) <999999
DROP TABLE testcase
SET NOCOUNT OFF
April 14, 2005 at 5:22 am
Amazing. Now I must find out what has changed in SP3a that caused this error
Thank you both!
--
Frank Kalis
Microsoft SQL Server MVP
Webmaster: http://www.insidesql.org/blogs
My blog: http://www.insidesql.org/blogs/frankkalis/[/url]
April 14, 2005 at 11:34 am
Since I am already working on the misteries caused by isnumeric & convert functions, I tried the following SQL & it worked !!!!
SET NOCOUNT ON
CREATE TABLE testcase
(
MyDecimal DECIMAL(2, 0) NOT NULL
)
CREATE INDEX ix_test ON testcase (MyDecimal)
INSERT INTO testcase VALUES (2)
SELECT * FROM testcase WITH (INDEX=0) WHERE MyDecimal <999999
SELECT * FROM testcase WHERE MyDecimal < convert(float,999999 )
SELECT * FROM testcase WHERE CAST(MyDecimal AS DECIMAL(6,0)) <999999
DROP TABLE testcase
SET NOCOUNT OFF
All I have done is added a convert() function
Please do not ask me how this worked... I am also searching for thesame answer
April 14, 2005 at 1:11 pm
To add to the confusion, (this second line errors on my server as well) I altered the third select to CAST MyDecimal to DECIMAL(2,0); its existing state... And that ran fine...
SET NOCOUNT ON
CREATE TABLE #testcase
(
MyDecimal DECIMAL(2, 0) NOT NULL
)
CREATE INDEX ix_test ON #testcase (MyDecimal)
INSERT INTO #testcase VALUES (2)
SELECT * FROM #testcase WITH (INDEX=0) WHERE MyDecimal <999999
--SELECT * FROM #testcase WHERE MyDecimal <999999
SELECT * FROM #testcase WHERE CAST(MyDecimal AS DECIMAL(2,0)) <999999
DROP TABLE #testcase
SET NOCOUNT OFF
I wasn't born stupid - I had to study.
April 15, 2005 at 12:48 am
But Fallell, I find that the statement which is giving error is still commented in your SQL
--SELECT * FROM #testcase WHERE MyDecimal <999999
Do you mean to say that even after uncommenting this line it worked fine ?
April 15, 2005 at 1:11 am
Yes, when you're explicite in your conversion it works. The commented line fails because SQL Server is trying to bring 999999 down to a DECIMAL(2,0) which obviously won't work. This FLOAT thingy works because FLOAT has a higher precedence. I'm a bit suprised that
SELECT * FROM #testcase WHERE CAST(MyDecimal AS DECIMAL(2,0)) <999999
works. I don't have an explanation for this right now
--
Frank Kalis
Microsoft SQL Server MVP
Webmaster: http://www.insidesql.org/blogs
My blog: http://www.insidesql.org/blogs/frankkalis/[/url]
April 15, 2005 at 1:11 pm
'zactly Frank. This is very perplexing. Even CASTing MyDecimal as its own datatype works...
I have forwarded this thread to our DBA. We deal with different numeric datatypes all the time and this is really odd?
How did you find this?
I wasn't born stupid - I had to study.
April 18, 2005 at 1:07 am
Another oddity!
Till now the column is declared NOT NULL. Change this to NULL and run
CREATE TABLE testcase
(
MyDecimal DECIMAL(2,0)
)
CREATE INDEX ix_test ON testcase (MyDecimal)
INSERT INTO testcase VALUES(1)
SELECT
*
FROM
testcase
WHERE
CAST(MyDecimal AS DECIMAL(2,0)) < 999999 --fails now
DROP TABLE testcase
(1 row(s) affected)
Server: Msg 8115, Level 16, State 8, Line 8
Arithmetic overflow error converting numeric to data type numeric.
I'll guess this will be my first posting in the private MVP newsgroup.
--
Frank Kalis
Microsoft SQL Server MVP
Webmaster: http://www.insidesql.org/blogs
My blog: http://www.insidesql.org/blogs/frankkalis/[/url]
April 29, 2005 at 7:35 am
Frank, I recently had the opportunity to try this on a machine that did not have SP3a. Got the same results...
I wasn't born stupid - I had to study.
Viewing 11 posts - 1 through 10 (of 10 total)
You must be logged in to reply to this topic. Login to reply