Solved

SQL Server 2008 Question

Posted on 2013-01-02
6
392 Views
Last Modified: 2013-01-03
The SQL below is giving me an error below:

Msg 3623, Level 16, State 1, Line 1
An invalid floating point operation occurred.

How can I fix this?

CASE WHEN   ISNUMERIC(WT.text)<> 1 OR ISNUMERIC(HT.text)<> 1
THEN NULL
ELSE
 CASE WHEN (Year(getdate())-#tmpClient.BirthYearNum) > = 18    
   THEN Convert(decimal(6,2),(Power(Convert(decimal(6,2),HT.text),0.725) * Power(convert(decimal(6,2),convert(float,WT.text)/1000.0),0.425)) * 0.007184)    
  ELSE Convert(decimal(6,2),SQRT((Convert(decimal(6,2),HT.text) * (convert(decimal(6,2),convert(float,WT.text)/3600)))))    
 END  
END as BSA ,
0
Comment
Question by:rustypoot
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 19

Assisted Solution

by:Rimvis
Rimvis earned 100 total points
ID: 38739797
Hi rustypoot,

ISNUMERIC does not guarantee that value can be successfully casted as a number.
You can implement custom function to check if the value is correct. Check this for reference:
http://classicasp.aspfaq.com/general/what-is-wrong-with-isnumeric.html
0
 
LVL 32

Accepted Solution

by:
Ephraim Wangoya earned 100 total points
ID: 38739941
Try this

CASE WHEN (WT.text LIKE '%[^0-9]%') AND (HT.text) LIKE '%[^0-9]%') THEN
  CASE WHEN (Year(getdate())-#tmpClient.BirthYearNum) > = 18  THEN 
      Convert(decimal(6,2),(Power(Convert(decimal(6,2),HT.text),0.725) *
         Power(convert(decimal(6,2),convert(float,WT.text)/1000.0),0.425)) * 0.007184)     
  ELSE 
      Convert(decimal(6,2),SQRT((Convert(decimal(6,2),HT.text) *  
         (convert(decimal(6,2),convert(float,WT.text)/3600)))))
  END
ELSE
  NULL
END as BSA

Open in new window

0
 
LVL 51

Assisted Solution

by:Mark Wills
Mark Wills earned 300 total points
ID: 38740048
Unfortunately isnumeric can return a 1 for any of the number types - which includes money and symbols like +,- and $

Have you been able to identify the data causing the problem ?

Does it need to be float ?

And the "like" wont really work either... e.g.

select case when 'abd 123' LIKE '%[^0-9]%' then 'use it' else 'lose it' end as [like]
      ,case when isnumeric('$12,300.00')=1 then 'use it' else 'lose it' end as [money]

Open in new window


You can see from the above that either will cause havoc.
0
Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

 
LVL 51

Assisted Solution

by:Mark Wills
Mark Wills earned 300 total points
ID: 38740896
Pity you arent using SQL2012 because you could use :

-- http://msdn.microsoft.com/en-us/library/hh230993.aspx

SELECT 
    CASE WHEN TRY_CONVERT(float, 'test') IS NULL 
    THEN 'Cast failed'
    ELSE 'Cast succeeded'
END AS Result;

Open in new window


I think for your purposes, you will most likely need a function, or possibly a series of PATINDEX() functions to make sure you are dealing with numbers that will convert (cast) properly...

Also, if the results of a CASE is meant to be decimal(6,2) then your NULL condition should also be expressed as a decimal(6,2)

Please let us know if you can find the data (ie it is a data problem and not really a function problem) or if you need a user defined function.

Only problem with a user defined function is it will be scaler and that can be slowwwww...

Maybe the query could be expressed as a stored procedure ? We might have a few more options as a SP.
0
 
LVL 51

Assisted Solution

by:Mark Wills
Mark Wills earned 300 total points
ID: 38741124
If you try to convert an inappropriate value, you normally expect a "conversion" failure, or, maybe an arithmetic overflow if the values are too big for decimal(6,2)

Your error is indicating something else...

Like a SQRT(-4) instead of SQRT(4), so might need to check for any negative values when you go through to check for data...
0
 

Author Closing Comment

by:rustypoot
ID: 38741222
Thanks everyone for your feedback! I appreciate it. <br /><br />After spending more time researching the issue, I found out that it was the POWER (Convert (6,2)) function that was causing this error! One of the users had entered the data that was longer than (6,2)! So, I modified the code from (6,2) to (30,2) and it worked! <br /><br />Thanks for all your time and feedback.
0

Featured Post

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

I have written a PowerShell script to "walk" the security structure of each SQL instance to find:         Each Login (Windows or SQL)             * Its Server Roles             * Every database to which the login is mapped             * The associated "Database User" for this …
Long way back, we had to take help from third party tools in order to encrypt and decrypt data.  Gradually Microsoft understood the need for this feature and started to implement it by building functionality into SQL Server. Finally, with SQL 2008, …
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

749 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