Can select float value but fail to insert it - sql-server

When I select I get the following value: 1.79769313486232E+308
The Column type is float.
When I'm trying to insert this value to the same table and column, I'm getting the following error:
The floating point value '1.79769313486232E+308' is out of the range of computer representation (8 byte)
When I select with master.dbo.fn_varbintohexstr(ColumnName) I'm getting
the hex value as 0x7fefffffffffffff which is less then 8 bytes...
Any explanation please?

Related

confuse SQL Server datatype decimal over 15 digit after comma

I have table with structure as follow :
Table1 (
id int NULL,
description varchar(50) null,
rate decimal(18,15) NULL
)
and when I test insert data into the table problem value like this :
insert into Table1 (id, description, rate)
values (1, 'My Room Upstair', 38397.0893181818)
and the error like this :
Msg 8115, Level 16, State 8, Line 1 Arithmetic overflow error
converting numeric to data type numeric.
But when I use Float data type the data inserted successful. But the rate value change to different value.
need advice please...
solution of decimal data type problem
The problem is that you are not using the decimal data type correctly.
The data type is defined like this decimal(a, b).
a is used to specify the amount of digits in the value. The default value is 18, but you can choose whatever number between 1, which is the minimum and 38, which is the maximum.
b is used for the amount of digits after the decimal point. The default value is 0 but you can specify choosing between the minimum and maximum, which are the same 1 and 38. b can only be specified if a is also specified. It needs to be equal or less than a.
In your example decimal(18,15), you can insert a number that is 18 digits long, 3 before and 15 after the decimal point. 38397.0893181818 has 5 digits before the decimal point, which is why you get the error. So if you need to add 38397.0893181818, you need decimal(15,10), since the number has 15 digits, 10 of which are after the decimal point.
The problem is that the decimal(a,b) parameters work according to what you need, a defines the number of total digits and b defines the number of digits of that total that will go after the decimal point.
https://learn.microsoft.com/es-es/sql/t-sql/data-types/decimal-and-numeric-transact-sql?view=sql-server-ver16

On Insert: Arithmetic overflow error converting expression to data type int

I tried creating my third table of phone numbers, I wrote the following commands:
CREATE TABLE MYPHONE (EMPLOYEE_ID INT, PHONE_NUMBER INT)
INSERT INTO MYPHONE
VALUES (1 , 7894561230)
But when I tried executing this previous INSERT command, I got an error:
Msg 8115, Level 16, State 2, Line 46
Arithmetic overflow error converting expression to data type int.
An int probably isn't the best data type for a phone number - you don't need to do any arithmetic on phone numbers, so why use a numerical data type? Store it as a string (or varchar in SQL).
Also, if you did need to store a value as an int, 2,147,483,647 is the maximum - anything higher would "overflow", hence the error you're getting. A long (or bigint in SQL) would allow values up to 9,223,372,036,854,775,808.
Allowed range of INT datatype is -2,147,483,648 to 2,147,483,647
You can get more details on the allowed range here
You are trying to insert beyond that range. For phone number better choose VARCHAR data type. If you still want to go for number, in that case use BIGINT instead of INT.

Cast giving an error

Why is this simple code giving me an error:
Conversion failed when converting the varchar value '3.0' to data type
int.
select cast(value as INT)
from CV3BasicObservation (nolock)
where value >= 110
SQL Server doesn't want to convert a number string that looks like a decimal to integer because you could lose precision. You could trick it with the round function:
select cast(round(value,0) as INT)
from CV3BasicObservation (nolock)
where cast(round(value,0) as INT) >= 110
NOTE: You have to do it to all instances of the field value where you are explicitly converting it to int or where it is implicitly converting it for comparison an int type value.

Data types to store Integer and Float values in SQL Server

What would be the best data type for storing Integer, Float values altogether in SQL Server.
I need to store normal integer and point (float) decimal values in a single column.
Ex.
ColumnNameValues
15
15.5
12.3
17
19.8
The best data type for storing Integer, Float values altogether in MySQL is : FLOAT
If you dont need to see the floats correctly in the DB, you could convert the 4 bytes of the float into an integer and store it as an integer. If you want to get it back, read it from the DB then convert it to the float again.
If you dont have the information from the context, if the stored value is an integer or float, it gets a little tricky, you would need to limit the integers or floats somehow, e.g. "sacrifice" one bit to identify, which variation it is.
if you are going to store in integer it will cut down the values after .(point)but if you going to use Float it will enter all the values whether it is int or float
declare #t table (ID float)
insert into #t(id)values (15) ,(15.5),( 12.3),( 17),( 19.8)
select * from #t
declare #t table (ID int)
insert into #t(id)values (15) ,(15.5),( 12.3),( 17),( 19.8)
select * from #t

SQL Server CAST from varchar to numeric fails with error

I have a column of data that contains 9 digits of numeric values, but the column is defined as a varchar. I need to CAST the field to a numeric value so I can divide the sum of the column by 100. e.g.
select CAST(field1 as numeric(9,2)) / 100 from table;
I get the following error when running the query: Arithmetic overflow error converting varchar to data type numeric.
If I perform a double CAST from varchar -> int -> numeric, the CAST works. e.g.
select CAST(CAST(field1 as int) as numeric(9,2)) / 100 from table;
Is there a reason why the single CAST from varchar -> numeric results in a SQL error, but the double CAST works?
If your field contains 9 digits, it could be a max of 999,999,999. My original thought was that your cast should be CAST (NUMERIC 11, 2)
edit
To be on the safe side, with 9 character length, you could have numbers ranging from 999,999,999 to 0.1234567. This means you need 9 digits before the decimal point and 7 after (total 16). Therefore your cast should be CAST (NUMERIC (16,7)
select CAST(field1 as numeric(16,7) from table;
The cause of the error was one row that had '-' as the value for the field. CASTing directly to NUMERIC doesn't work, but CASTing to INT first forced the '-' fields to return 0.

Resources