Solved

convert real to in

Posted on 2009-07-03
9
284 Views
Last Modified: 2012-05-07
Hi Experts, I'm having a problem I don't understand. I need to cast a real to a integer and I test on the maximum possible int value. When I do casting I get the "Arithmetic overflow error for type int, value = 2147483648.000000"

Check the code

tnx leon
DECLARE @myFloatToInt AS FLOAT
SET @myFloatToInt = 2147483647
 
DECLARE @myFloatToIntToBig AS FLOAT
SET @myFloatToIntToBig = 6666666666
 
DECLARE @myRealToInt AS REAL
SET @myRealToInt = 2147483647
 
DECLARE @myRealToIntToBig AS REAL
SET @myRealToIntToBig = 6666666666
 
 
SELECT  CASE WHEN @myFloatToInt > 2147483647 THEN 2147483647
             ELSE @myFloatToInt
        END AS myFloatToInt
      , CASE WHEN @myFloatToIntToBig > 2147483647 THEN 2147483646
             ELSE @myFloatToIntToBig
        END AS myFloatToBig
      , CASE WHEN @myRealToInt > 2147483647 THEN 2147483647
--             ELSE @myRealToInt
             ELSE CAST(@myRealToInt AS INT)
        END AS myRealInt
      , CASE WHEN @myRealToIntToBig > 2147483647 THEN 2147483646 
             ELSE @myRealToIntToBig 
        END AS myRealToBig

Open in new window

0
Comment
Question by:sojoca
  • 4
  • 3
  • 2
9 Comments
 
LVL 23

Accepted Solution

by:
Racim BOUDJAKDJI earned 350 total points
ID: 24771709
From memory and if I am not mistaken the value 2147483648.000000 is above the defined threshold for int.  Please cast as bigint instead.

HTH
0
 
LVL 14

Assisted Solution

by:Jagdish Devaku
Jagdish Devaku earned 150 total points
ID: 24771726
hi check the below code... i have changed INT to BIGINT in the above code in CAST.
please let me know if there is any issue.
bye.

DECLARE @myFloatToInt AS FLOAT
SET @myFloatToInt = 2147483647
 
DECLARE @myFloatToIntToBig AS FLOAT
SET @myFloatToIntToBig = 6666666666
 
DECLARE @myRealToInt AS REAL
SET @myRealToInt = 2147483647
 
DECLARE @myRealToIntToBig AS REAL
SET @myRealToIntToBig = 6666666666
 
 
SELECT  CASE WHEN @myFloatToInt > 2147483647 THEN 2147483647
             ELSE @myFloatToInt
        END AS myFloatToInt
      , CASE WHEN @myFloatToIntToBig > 2147483647 THEN 2147483646
             ELSE @myFloatToIntToBig
        END AS myFloatToBig
      , CASE WHEN @myRealToInt > 2147483647 THEN 2147483647
--             ELSE @myRealToInt
             ELSE CAST(@myRealToInt AS BIGINT)
        END AS myRealInt
      , CASE WHEN @myRealToIntToBig > 2147483647 THEN 2147483646 
             ELSE @myRealToIntToBig 
        END AS myRealToBig

Open in new window

0
 
LVL 23

Expert Comment

by:Racim BOUDJAKDJI
ID: 24771736
I'd suggest making all compares in the same data type (preferably the one with the largest capacity) to avoid data type precedence cast issues.
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 23

Expert Comment

by:Racim BOUDJAKDJI
ID: 24771749
Please read the following to understand what data type precedence is.

http://msdn.microsoft.com/en-us/library/ms190309.aspx
HTH
0
 

Author Comment

by:sojoca
ID: 24771848
let me re-phrase: I'm writing some adaptor in SISS and the real-field and the integer-field are given; I can't change the type. Because the real-field can contain a bigger numeric value then the integer-field I need to check the max value as in:
SELECT  CASE WHEN @myFloatToInt > 2147483647 THEN 2147483647
             ELSE @myFloatToInt
        END AS myFloatToInt
But when the value is bigger than 2147483647 than iit gets rounded to 2147483648.000000 (which is bigger than the max int-value 2147483647)
0
 
LVL 23

Expert Comment

by:Racim BOUDJAKDJI
ID: 24771997
You need to do explicit casting...

SELECT  CASE WHEN @myFloatToInt > cast(2147483647 as real) THEN 2147483647
             ELSE @myFloatToInt
        END AS myFloatToInt
0
 

Author Comment

by:sojoca
ID: 24772083
hi Racimo, still doesn't solve my problem. Check

--             ELSE @myRealToInt  --  is OK
             ELSE CAST(@myRealToInt AS INT)  -- goes wrong

in the code snippet

DECLARE @myFloatToInt AS FLOAT
SET @myFloatToInt = 2147483647
 
DECLARE @myFloatToIntToBig AS FLOAT
SET @myFloatToIntToBig = 6666666666
 
DECLARE @myRealToInt AS REAL
SET @myRealToInt = 2147483647
 
DECLARE @myRealToIntToBig AS REAL
SET @myRealToIntToBig = 6666666666
 
SELECT  CASE WHEN @myFloatToInt > 2147483647 THEN 2147483647
             ELSE @myFloatToInt
        END AS myFloatToInt
      , CASE WHEN @myFloatToIntToBig > 2147483647 THEN 2147483646
             ELSE @myFloatToIntToBig
        END AS myFloatToBig
      , CASE WHEN @myRealToInt > CAST(2147483647 AS REAL) THEN 2147483647
--             ELSE @myRealToInt  --  is OK
             ELSE CAST(@myRealToInt AS INT)  -- goes wrong
        END AS myRealInt
      , CASE WHEN @myRealToIntToBig > 2147483647 THEN 2147483646 
             ELSE CAST(@myRealToIntToBig AS INT)
        END AS myRealToBig

Open in new window

0
 
LVL 14

Expert Comment

by:Jagdish Devaku
ID: 24772103
hi,
cast to bigint will solve the issue. but the value might crossed the range of bigint. I suggest you to keep it to float or cast it to nvarchar or so...
bye
0
 

Author Closing Comment

by:sojoca
ID: 31599529
I didn't "see" the solution rightaway, hence my re-phrase. Extra words of the experts would have helped me. I was to busy to see I guess
0

Featured Post

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
View SQL 2005 Job package 16 43
T-SQL: Do I need CLUSTERED here? 13 44
VB.net and sql server 4 36
SQL - Update field defined as Text 6 17
Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question