An invalid floating point operation occurred POWER SQL Server - sql-server

I am have a problem with the code, when I run I get
An invalid floating point operation occurred
My code is the following:
select POWER( ISNULL(value1,0) / NULLIF(value2,0) , 12 / CONVERT(numeric(6,2),value3))
where value1(data type: money), value2(data type: money), value3(data type: int) are columns in my database.
I get that error when value1 = Null, value2 = 164083520.00 and value3 = 177
The weird is that if I replace with these numbers I get right result, but not while running this.
Any ideas?

Executing the following script I'm not getting any errors on SQL Server 2012. AFAICT you won't get any on other versions either. You must be having a calculation somewhere else that results in an invalid floating point operation.
DECLARE #values TABLE(value1 MONEY,value2 MONEY,value3 INT);
INSERT INTO #values(value1,value2,value3)VALUES(NULL,164083520.00,177);
SELECT
result=POWER(ISNULL(value1,0)/NULLIF(value2,0), 12.0/CONVERT(numeric(6,2),value3))
FROM
#values;
Result
0.00

Related

How to assign decimal value to string in SQL Server 2012

I'm converting a formula from Crystal Reports into a Sql View.
The SQL Syntax that produced the decimal output was this:
if {ACCOUNT_INFO.InstitutionIdentifier}="TPOCFBOI" then 1.10 else
if {ACCOUNT_INFO.InstitutionIdentifier}="TPOBCB"then 0.50 else
if {ACCOUNT_INFO.InstitutionIdentifier}="TPOCFB"then 0.75 else
if {INSTITUTION.Region}="WMG" then .01 else
if {vAM_CRG_LOAN_OFFICER_NAME.LoanOfficerName} ="Margaret S. Smith"
then .875
What would the code look like in a Sql View to produce a decimal (3
places)?
I tried
DECLARE
#LO_Individual_Comp DECIMAL(18,3),
BEGIN
SELECT description INTO LO_Individual_Comp
FROM {ACCOUNT_INFO}
WHERE InstitutionIdentifier} = "TPOCFBOI";
DBMS_OUTPUT.PUT_LINE('1.10' || LO_Individual_Comp;
END;
If > Then Sql Equivalent to return a decimal (3 places)
You could use a CASE statement
select
case
when ACCOUNT_INFO.InstitutionIdentifier ='TPOCFBOI' then 1.10
when ACCOUNT_INFO.InstitutionIdentifier ='TPOBCB' then 0.50
when ACCOUNT_INFO.InstitutionIdentifier ='TPOCFB' then 0.75
when INSTITUTION.Region = 'WMG' then .01
when vAM_CRG_LOAN_OFFICER_NAME.LoanOfficerName ="Margaret S. Smith" then .875
END as 'Decimal'
from...
Now you missing detail on how your tables would join. It appears that your trying to compare data from three different tables (ACCOUNT_INFO, INSTITUTION, vAM_CRG_LOAN_OFFICER_NAME).

Pyodbc - Inserting values into SQL Server db error

I am trying to use python to insert values into a table.
TableName = "Table_Test1"
This is the code that I am trying:
cursor.execute("Insert into " + TableName + " (TestCol1,TestCol2) VALUES (?,?)", Val1, Val2)
What is the mistake that I am making here?
It is giving me this error - <class 'pyodbc.ProgrammingError'> returned a result with an error set
TestCol2 in the db was declared as nvarchar and the Val2 that the program returned was 0 which is an integer. I changed the datatype in the db and then the program started working.

Why do I get "Conversion failed when converting date and/or time from character string" when subtracting a number from GETDATE()?

I cannot figure out what I am doing incorrect here. I have the following values for these variables.
my $sql = qq~
SELECT COUNT(*)
FROM tableName u
WHERE 1=1 AND u.ManufacturerPartNumber IN ('X','Y','Z')
AND CAST(u.InspectionStartDate AS DATETIME) > (GETDATE() - ?)
~;
my $argsRef->{lookBack} = 30;
And when I try to run a selectrow_array on the sql like such:
my $qnCount = $dbh->selectrow_array($sql, undef, $argsRef->{lookBack});
I get the following error:
DBD::ODBC::db selectrow_array failed: [Microsoft][ODBC SQL Server Driver][SQL Server]Conversion failed when converting date and/or time from character string. (SQL-22007) [for Statement "
SELECT COUNT(*)
FROM tableName u
WHERE 1=1 AND u.ManufacturerPartNumber IN ('X','Y','Z')
AND CAST(u.InspectionStartDate AS DATETIME) > (GETDATE() - ?)
"]
So, it is my understanding that the third parameter in the selectrow_array call from a database handle should be the bind variables. Yet, they seem not to be binding....
the database handle is created as such:
my $dbh = DBI->connect_cached("dbi:ODBC:$dsn", undef, undef, {
PrintError => 0,
RaiseError => 1,
ShowErrorStatement => 1,
LongReadLen => 500000,
})
$dsn is the correct DSN but not shown for various reasons, but we know it works due the error message coming back from SQL Server.
Any idea what I am doing wrong?
Thanks in advance for your help.
First, a shout out to ThisSuitIsBlackNot for pointing out that the problem. I made the assumption that the DBI/driver would be able to determine the value type. That was wrong. So, I explicitly stated the type:
$sth = $dbh->prepare($sql);
$sth->bind_param( 1, ($argsRef->{lookBack} * -1), { TYPE => SQL_INTEGER });
$sth->execute();
my $qnCount = $sth->fetchrow_array();
This allowed it to go through as an integer and the SQL ran without problem.
In case you were wondering why I am multiplying by -1, is because I changed the query to use DATEADD:
SELECT COUNT(*)
FROM tableName u
WHERE 1=1 AND u.ManufacturerPartNumber IN ('x')
AND CAST(u.InspectionStartDate AS DATETIME) > DATEADD(dd, ?, GETDATE())
Although there seems to be some continuing discussion in regards to its use, it is working here.
Thank you all for your help.

What cause the error 'Arithmetic overflow error converting varchar to data type numeric'?

I use the query below to convert a column 'Characteristic__c' of varchar(255) into DECIMAL(22,8). The value of Characteristic__c was specified as simply 0.0, but it still cause error 'Arithmetic overflow error converting varchar to data type numeric.'. Don't know the reason and thanks for help.
SELECT CONVERT(DECIMAL(22,8) , Characteristic__c) FROM [ODS].[dbo].
[Scorecard_Details__c]
WHERE Attribute__c='Employment Duration' and Characteristic__c=0.0 and
WOE__c=0
Presumably, Characteristic_c cannot be converted to a numeric. In SQL Server 2012+, you can simply use TRY_CONVERT():
SELECT TRY_CONVERT(DECIMAL(22, 8) , Characteristic__c)
FROM [ODS].[dbo].[Scorecard_Details__c]
WHERE Attribute__c = 'Employment Duration' AND
TRY_CONVERT(DECIMAL(22, 8), Characteristic__c) = 0.0 AND
WOE__c = 0;
Note: If WOE__c is a character column, you should use TRY_CONVERT() there too.
It looks like the number in the varchar is longer than decimal (22). I mean a number too high to be stored in decimal. Check your data

Entity Framework / SQL Server strange decimal division behaviour

I have a table in my SQL server 2008 R2 database which includes two nullable decimal(16,6) columns. Let's call them column1 and column2.
When I try to run a Linq query against the entity generated from this table:
Table.Select(r => new Foo
{
Bar = (r.Column1 + r.Column2) / 2m
}
);
I get a System.OverflowException if column1 + column2 >= 15846. The message of the exception is only:
Conversion overflows.
With a bit of trial and error I've managed to make the query work with the following:
Table.Select(r => new Foo
{
Bar = (r.Column1 + r.Column2).HasValue ?
(r.Column1 + r.Column2).Value / 2m : 0
}
);
However, I was wondering if anyone could explain what was going wrong with the initial query.
Edit
The first query generates this SQL:
SELECT
1 AS [C1],
([Extent1].[Column1] + [Extent1].[Column2]) / cast(2 as decimal(18)) AS [C2]
FROM [dbo].[Table] AS [Extent1]
With a value of 10000 for both columns, running the query manually in SSMS the result is 10000.0000000000000000000000000 (25 decimal zeros).
The second query has this SQL:
SELECT
1 AS [C1],
CASE WHEN ([Extent1].[Column1] + [Extent1].[Column2] IS NOT NULL)
THEN ([Extent1].[Column1] + [Extent1].[Column2]) / cast(2 as decimal(18))
ELSE cast(0 as decimal(18))
END AS [C2]
FROM [dbo].[Table] AS [Extent1]
Running the query in SSMS returns 10000.00000000000000000000 (20 decimal zeros). Apparently there is a problem when EF tries to convert the first value (with 25 decimal zeros) into a decimal but with the second (with 20 decimal zeros) it works.
In the meantime it turned out that the problem also occurs with non-nullable columns and even a single decimal(16, 6) column. The following ...
Table.Select(r => new Foo
{
Bar = r.Column1 / 2m
}
);
... throws the same conversion exception (with a value of 20000 in the Column1).
Why do those two SQL queries result in two different numbers of digits?
And why can't the first number be converted into a decimal by EF?

Categories

Resources