Write Decimal numbers in SQL 2008 - sql

In need to write the number 7.2 in a table field.
What type the field needs to be.
And what type needs the variable to be?

SQL Server 2008 has a DECIMAL type which works quite well.
You can define a precision (total number of digits) and a scale (number of digits after the decimal separator).
DECIMAL(2,1) would be: two digits, one of which after the separator (one before)
DECIMAL(5,2) would be: five figits total, two of which after the separator (three before)
See DECIMAL and NUMERIC in SQL Server Books Online for more details
In .NET, this would be a type decimal as well.

Related

Want to extract digits before decimal and after decimal as string in spark SQL

I have a number like this 000123.00 (lets call it myvar)
I want to use SPARK SQL to get following two results
000123 (Digits before decimal point)
2 (Count of digits after decimal point)
Note: I want the solution in a single SELECT statement.
You can cast the number to string and use split to get both left and right side of decimal point, and then use char_length function to retrieve the number of digits after decimal point.

Find float column max scale and precision

I have a column with datatype float in Teradata. I want to find the Maximum precision and scale for that column.
Note: My column's scale part has more than 10 digits in most of the places.
Sample Data
123.12321323002
13123213.13200003
33232.213123001
The output I need is
Precsion 19 (scale + length of 13123213) and scale is 11 (length of 12321323002)
or
8 (length of 13123213), 11 (length of 12321323002).
I tried to find them buy converting the column as varchar and splitting them based on the '.' and make the integer and fractional part as 2 columns and then finding the max length of 2 columns. But when I'm select the data, Teradata rounds off the scale part. So after that, if I convert them as char, I'm getting lesser value for scale part.
For example:
org data: 1234.12312000123101
data when I select from Teradata: 1234.12312000123
This is a bit long for a comment.
Teradata uses the IEEE format for real/float values. This gives 15-17 digits of precision. Alas, you need 19 digits, so the values you want are not being stored in the database. You cannot recover them.
What you can do is fix the database, so it uses numeric/decimal/number. This supports what you want: NUMERIC(19, 11). You would then need to reload the data so it is correctly stored in the database.
When you need high precision without predefined scale simply switch to the NUMBER datatype, which is a mixture of DECIMAL and FLOAT.
Exact numeric, at least 38 digits precision, no predefined scale, range of 1E-130 .. 1E125.
Float on steroids :-)

Excel MSsql different values

I have create a table named:
sub
,with field:
addn numeric (24,6) NULL
I have used the values in this txt file to insert.
https://drive.google.com/file/d/1z2ixHDOvHiM5bqDSI3fCbkuXa0Syfjrn/view
Question:
Why is it the if I query this:
select SUM(addn) from sub
Result:
131546008007.610000
and if I paste the result of this in Excel:
select * from sub
the sum is:
131546008007.57
Note:
there are 4 (-0.01) in the query. I don't know if this is trigger and how to solve this
This is topic about Precision, Scale and Length. Numeric datatype is stored/managed in different way than float datatype, for instance.
Try this query, and you will have the same result than in Excel:
select sum(cast(addn as float)) from sub
131546008007.57
Here you have some links where they are explaining that the float datatype is an approximate number, and the decimal is more accurate than the float datatype. So you can see with this than Excel is using approximate numbers.
Precision, Scale, and Length
Here they are explaining than in financial applications you should NOT use floating-point datatypes, so it is good you're using numeric in your DB, and therefore you can rely on your SQL DB in this example.
And here they state that:
Excel was designed in accordance to the IEEE Standard for Binary Floating-Point Arithmetic (IEEE 754). The standard defines how floating-point numbers are stored and calculated. The IEEE 754 standard is widely used because it allows-floating point numbers to be stored in a reasonable amount of space and calculations can occur relatively quickly.
.
Excel store 15 significant digits of precision.

Database Data Types

What database data type do I need to use to store a numerical value with 18 digits after the decimal place? I've tried double and float but it doesn't work it only shows 16 digits after the decimal place. I'm using MS SQL Server 2005 and 2008.
Assuming your values are not too big, you want to use DECIMAL. Something like DECIMAL(20, 18) (adjust the first value for the total number of digits in the number).
You can read about it here.

Select all rows where a varchar column converts to a decimal

I have a varchar column that has generally has a decimal value, but some times there is some garbage text characters in that field.
Is it possible to filter in the WHERE clause for rows that sucessfully convert to a decimal value?
I am using sql-server 2005
One way is the ISNUMERIC function:
select * from YourTable where ISNUMERIC(col1) = 1
There's one gotcha: isnumeric returns 1 whenever a string can be converted to any numeric type, including money. For example, say you have rows using varying decimal separators, like 7.9 and 7,9. Both will convert to money, and isnumeric returns 1 for both of them. But only one converts to decimal, depending on the SQL Server language settings.