Good way to format decimal in SQL Server - sql

We store a decimal(9,8) in our database. It can have any number of places after the decimal point (well, no more than 8). I am frustrated because I want to display it as human-readable text as part of a larger string created on the server. I want as many decimals to the right of the decimal point as are non-zero, for example:
0.05
0.12345
3.14159265
Are all good
If I do
CAST(d AS varchar(50))
I get formatting like:
0.05000000
0.12345000
3.14159265
I get similar output if I cast/convert to a float or other type before casting to a varchar. I know how to do a fixed number of decimal places, such as:
0.050
0.123
3.142
But that is not what I want.
Yes, I know I can do this through complicated string manipulation (REPLACE, etc), there should be a good way to do it.

Playing around (sql server) i find that casting to float first makes the trick ..
select cast( cast(0.0501000 as float) as varchar(50) )
yields
0.0501

Code copied almost verbatim from here (also discusses the 6-digit limit on float formatting in mode 0):
DECLARE #num3 TABLE (i decimal(9, 8))
INSERT #num3
SELECT 0.05
UNION ALL
SELECT 0.12345
UNION ALL
SELECT 3.14159265
SELECT i
,CASE WHEN PATINDEX('%[1-9]%', REVERSE(i)) < PATINDEX('%.%', REVERSE(i))
THEN LEFT(i, LEN(i) - PATINDEX('%[1-9]%', REVERSE(i)) + 1)
ELSE LEFT(i, LEN(i) - PATINDEX('%.%', REVERSE(i)))
END 'Converted'
FROM #num3

For anything other than fairly straight forward manipulation, I'd be considering doing this in your calling code instead tbh as I think it's usually best for SQL to return the data as-is from the database, and then leave the formatting of that up to whatever is calling it, which is more than likely better geared up for string manipulation. Especially if you find yourself jumping though hoops to try to achieve it.

Related

Issue converting JSON string into decimal

This question may answer on many threads but I am unable to find answer specific to my problem.
Q: I am getting data from API (in json format) where all columns are coming as string and inserting into a table which has all columns as string and serving as source table.
Now, I am trying to cast data from that source to destination and making all necessary casting to insert data into destination table. But decimal (16,8) casting failed.
I debug issue at my end and found that during the fetching data from API which is returning the data into json format converting values in some unusual format.
For e.g. 0.00007 converting into 7E-05 and this is happening for many other rows.
I know I can fix this problem at API implementation level. But I asked to solve this at SQL server end. So I need a solution which should convert 7E-05 into 0.00007.
Try something like:
SELECT CAST(CAST(#t AS FLOAT) AS DECIMAL(16,8))
Results in:
0.00007000
CAST to a FLOAT, then to a DECIMAL.
This unusual format is a rather usual scientific notation Wikipedia, read section "E-notation"
You see the E and a number meaning exponent.
"1E2" = 1 * 10^2 = 100
"1E-2" = 1 * 10^(-2) = 0.01
Try this out:
DECLARE #tbl TABLE(Numberstring VARCHAR(100));
INSERT INTO #tbl VALUES('100'),('1E2'),('1E-2'),('7E-05');
SELECT Numberstring
,CAST(Numberstring AS FLOAT)
,CAST(CAST(Numberstring AS FLOAT) AS DECIMAL(20,10))
FROM #tbl;
The result
100 100 100.0000000000
1E2 100 100.0000000000
1E-2 0,01 0.0100000000
7E-05 7E-05 0.0000700000
You can see, that the FLOAT type itself will display the last one in the scientific notation, while the cast to DECIMAL will return the number you are expecting.
I'd be happy with an upvote, but you should accept Shawn's answer as it was earlier than mine :-D

Trim decimal off a varchar value

I've got a legacy SQL Server stored procedure that stopped working some time ago. While looking at it today, there is an inner join where one table is storing the value as an int and the other is storing it as a varchar in a (##.#) format. Not sure why or how that happened but SQL Server is none too happy about it.
I need a simple programmatic bit of string manipulation to pull out everything to the left of the decimal point so I can cast or convert it to an int to fix the join.
I started with the following, however substring requires a fixed length and the data could be 1-3 digits to the left of the decimal. Having trouble with the dynamic aspect of it. For clarity sake, I don't care what's to the right of the decimal.
cast(substring(H.Variable, 1, 1) as int)
First, find the index of the decimal by using CHARINDEX(). Then, you can pass that index to the LEFT() function:
LEFT(H.Variable, CHARINDEX('.', H.Variable) - 1)
Try:
CAST(TRY_CAST H.Variable AS Float) AS Int)
That should get you the integer value of the varchar string--if it cannot be converted, it will come back as NULL.
It's going in the other direction than your question, but is likely to be more accurate and higher performance.
Note that you need SQL Server 2012 or later to use the TRY_CAST conversion...
If you can have no decimals with decimals, you need to account for that.
declare #table table (c1 varchar(64))
insert into #table
values
('123')
,('5465465.465465')
select
case when CHARINDEX('.', c1) = 0 then c1 else LEFT(c1, CHARINDEX('.', c1) - 1) end
from #table
Other wise, only using LEFT() and CHARINDEX() will result in:
Invalid length parameter passed to the LEFT or SUBSTRING function.
Another way is
substring(c1,0,case when charindex('.',c1) = 0 then 9999 else charindex('.',c1) end)
Try:
CONVERT(INT, H)
It could be more tolerant...

Decimals missing in Division Output

I'm a rookie SQL Programmer, but have searched here and many other SQL Forums and can't figure out why my simple division script is still ignoring the decimals. I've CAST EVERYTHING as Decimal, but still don't get any in the output . .
(CAST((ABS(CAST(CAST(SUM(h4qqnb) AS DECIMAL(12,4)) - CAST(SUM(h4hdnb) AS DECIMAL(12,4))AS DECIMAL(12,4)))/CAST(SUM(h4hdnb) AS DECIMAL(12,4))) AS DECIMAL(10,2))*100)||'%' AS Count_Variance_Rate,
What am I missing?
thanks!
That's a seriously ugly expression, with far too much CASTing, but essentially what you're doing (just looking at the outermost CAST statement) is saying
CAST(someNumber as DECIMAL(10,2)
which is going to give you a number with two decimal places of precision. You're then multiplying that by 100, which is going to give you an integer.
If you're trying to get a percentage value formatted to two decimal places, you can do it like this, assuming that h4qqnb and h4hdnb are decimal fields to begin with:
concat(cast(cast(abs(sum(h4qqnb) - sum(h4hdnb)) / sum(h4hdnb) as decimal(10, 4)) * 100 as decimal(10, 2)), '%') as Count_Variance_Rate2
Working example at http://sqlfiddle.com/#!2/279752/5/0

How do I count decimal places in SQL?

I have a column X which is full of floats with decimals places ranging from 0 (no decimals) to 6 (maximum). I can count on the fact that there are no floats with greater than 6 decimal places. Given that, how do I make a new column such that it tells me how many digits come after the decimal?
I have seen some threads suggesting that I use CAST to convert the float to a string, then parse the string to count the length of the string that comes after the decimal. Is this the best way to go?
You can use something like this:
declare #v sql_variant
set #v=0.1242311
select SQL_VARIANT_PROPERTY(#v, 'Scale') as Scale
This will return 7.
I tried to make the above query work with a float column but couldn't get it working as expected. It only works with a sql_variant column as you can see here: http://sqlfiddle.com/#!6/5c62c/2
So, I proceeded to find another way and building upon this answer, I got this:
SELECT value,
LEN(
CAST(
CAST(
REVERSE(
CONVERT(VARCHAR(50), value, 128)
) AS float
) AS bigint
)
) as Decimals
FROM Numbers
Here's a SQL Fiddle to test this out: http://sqlfiddle.com/#!6/23d4f/29
To account for that little quirk, here's a modified version that will handle the case when the float value has no decimal part:
SELECT value,
Decimals = CASE Charindex('.', value)
WHEN 0 THEN 0
ELSE
Len (
Cast(
Cast(
Reverse(CONVERT(VARCHAR(50), value, 128)) AS FLOAT
) AS BIGINT
)
)
END
FROM numbers
Here's the accompanying SQL Fiddle: http://sqlfiddle.com/#!6/10d54/11
This thread is also using CAST, but I found the answer interesting:
http://www.sqlservercentral.com/Forums/Topic314390-8-1.aspx
DECLARE #Places INT
SELECT TOP 1000000 #Places = FLOOR(LOG10(REVERSE(ABS(SomeNumber)+1)))+1
FROM dbo.BigTest
and in ORACLE:
SELECT FLOOR(LOG(10,REVERSE(CAST(ABS(.56544)+1 as varchar(50))))) + 1 from DUAL
A float is just representing a real number. There is no meaning to the number of decimal places of a real number. In particular the real number 3 can have six decimal places, 3.000000, it's just that all the decimal places are zero.
You may have a display conversion which is not showing the right most zero values in the decimal.
Note also that the reason there is a maximum of 6 decimal places is that the seventh is imprecise, so the display conversion will not commit to a seventh decimal place value.
Also note that floats are stored in binary, and they actually have binary places to the right of a binary point. The decimal display is an approximation of the binary rational in the float storage which is in turn an approximation of a real number.
So the point is, there really is no sense of how many decimal places a float value has. If you do the conversion to a string (say using the CAST) you could count the decimal places. That really would be the best approach for what you are trying to do.
I answered this before, but I can tell from the comments that it's a little unclear. Over time I found a better way to express this.
Consider pi as
(a) 3.141592653590
This shows pi as 11 decimal places. However this was rounded to 12 decimal places, as pi, to 14 digits is
(b) 3.1415926535897932
A computer or database stores values in binary. For a single precision float, pi would be stored as
(c) 3.141592739105224609375
This is actually rounded up to the closest value that a single precision can store, just as we rounded in (a). The next lowest number a single precision can store is
(d) 3.141592502593994140625
So, when you are trying to count the number of decimal places, you are trying to find how many decimal places, after which all remaining decimals would be zero. However, since the number may need to be rounded to store it, it does not represent the correct value.
Numbers also introduce rounding error as mathematical operations are done, including converting from decimal to binary when inputting the number, and converting from binary to decimal when displaying the value.
You cannot reliably find the number of decimal places a number in a database has, because it is approximated to round it to store in a limited amount of storage. The difference between the real value, or even the exact binary value in the database will be rounded to represent it in decimal. There could always be more decimal digits which are missing from rounding, so you don't know when the zeros would have no more non-zero digits following it.
Solution for Oracle but you got the idea. trunc() removes decimal part in Oracle.
select *
from your_table
where (your_field*1000000 - trunc(your_field*1000000)) <> 0;
The idea of the query: Will there be any decimals left after you multiply by 1 000 000.
Another way I found is
SELECT 1.110000 , LEN(PARSENAME(Cast(1.110000 as float),1)) AS Count_AFTER_DECIMAL
I've noticed that Kshitij Manvelikar's answer has a bug. If there are no decimal places, instead of returning 0, it returns the total number of characters in the number.
So improving upon it:
Case When (SomeNumber = Cast(SomeNumber As Integer)) Then 0 Else LEN(PARSENAME(Cast(SomeNumber as float),1)) End
Here's another Oracle example. As I always warn non-Oracle users before they start screaming at me and downvoting etc... the SUBSTRING and INSTRING are ANSI SQL standard functions and can be used in any SQL. The Dual table can be replaced with any other table or created. Here's the link to SQL SERVER blog whre i copied dual table code from: http://blog.sqlauthority.com/2010/07/20/sql-server-select-from-dual-dual-equivalent/
CREATE TABLE DUAL
(
DUMMY VARCHAR(1)
)
GO
INSERT INTO DUAL (DUMMY)
VALUES ('X')
GO
The length after dot or decimal place is returned by this query.
The str can be converted to_number(str) if required. You can also get the length of the string before dot-decimal place - change code to LENGTH(SUBSTR(str, 1, dot_pos))-1 and remove +1 in INSTR part:
SELECT str, LENGTH(SUBSTR(str, dot_pos)) str_length_after_dot FROM
(
SELECT '000.000789' as str
, INSTR('000.000789', '.')+1 dot_pos
FROM dual
)
/
SQL>
STR STR_LENGTH_AFTER_DOT
----------------------------------
000.000789 6
You already have answers and examples about casting etc...
This question asks of regular SQL, but I needed a solution for SQLite. SQLite has neither a log10 function, nor a reverse string function builtin, so most of the answers here don't work. My solution is similar to Art's answer, and as a matter of fact, similar to what phan describes in the question body. It works by converting the floating point value (in SQLite, a "REAL" value) to text, and then counting the caracters after a decimal point.
For a column named "Column" from a table named "Table", the following query will produce a the count of each row's decimal places:
select
length(
substr(
cast(Column as text),
instr(cast(Column as text), '.')+1
)
) as "Column-precision" from "Table";
The code will cast the column as text, then get the index of a period (.) in the text, and fetch the substring from that point on to the end of the text. Then, it calculates the length of the result.
Remember to limit 100 if you don't want it to run for the entire table!
It's not a perfect solution; for example, it considers "10.0" as having 1 decimal place, even if it's only a 0. However, this is actually what I needed, so it wasn't a concern to me.
Hopefully this is useful to someone :)
Probably doesn't work well for floats, but I used this approach as a quick and dirty way to find number of significant decimal places in a decimal type in SQL Server. Last parameter of round function if not 0 indicates to truncate rather than round.
CASE
WHEN col = round(col, 1, 1) THEN 1
WHEN col = round(col, 2, 1) THEN 2
WHEN col = round(col, 3, 1) THEN 3
...
ELSE null END

SQL Convert float to comma varchar with decimals

I have a couple floats that are kinda big. They're around a 100 million.
I would like this number to show like the following 123,456,789.01234
I've found that I can use CONVERT if its a money datatype but this doesn't do the full trick (it leaves off some decimal places).
I have to have commas on the left and five decimal places on the right.
Is there any built in SQL function to help with this? Or do I have to write a custom function?
Thanks
*** Update
I forgot to mention that I'm just displaying these as varchars. So there isn't any calculations after this.
This is running on an SQL database so MySQL and Oracle won't work.
DECLARE #f FLOAT
SET #f = 123456789.01234
SELECT LEFT('$' + CONVERT(VARCHAR(20), CAST(#f AS MONEY), 1), LEN(#f) - 2)
this will cut it up to two places of decimal for formatting. You can change LEN(#f) - 2 to modify this setting.
if you are just displaying this as text you can do the following:
oracle :
select to_char(123456789.01234,'999,999,999.99999') from dual; => 123,456,789.01234
MySQL :
select format(123456789.01234,5) => 123,456,789.01234<br>
the MySQL function rounds