Need dynamic decimal places in SQL display - sql

In a display column I'm getting values as follows:
12.000000
12.350000
13.230000
14.560000
I need to represent these values with dynamic decimal places upto 2 places i.e if there are zero's then it should ignore.
for example: 12.35 only , if its 12.500000 then it should display 12.5 only

float type removes trailing zeros, at least for SQL Server. Try casting your decimal to float.
SELECT CAST(12.350000 as float)
Returns
12,35

You could try the following (ANSI standard SQL):
SELECT TRIM(TRAILING '0' FROM TRIM(CAST(CAST(myvalue AS DECIMAL(12,2)) AS CHAR(30))))
FROM mytable;
I'm not sure what flavor of SQL you're using but the above should work (the extra TRIM() is on the off-chance that the result of the CAST() is padded with spaces.

Related

SQL , format all numbers to 2 decimal places (eg 20 to 20.00)

I have a data set with inconsistencies in a column with double values. Some are displayed as eg. 24.55, and others as 24.5 or 24. I want all values to be displayed to 2 decimals, so 24 should be 24.00, and 23.1 should be 23.10 etc. What code would work in this instance?
In general, such conversions are both database-specific and GUI-specific. However, the database can convert the value to something with two decimal places by using numeric/decimal (those are equivalent):
select cast(value as numeric(10, 2))
The "2" is the two digits after the decimal place. This should be displayed with two digits -- in any reasonable interface.
If you are using MySQL (as PHP suggests), you can use the format() function to accomplish this:
select format(value, 2)

Currency column with no decimals: add in decimal

I've been reviewing a currency column that has no decimal spaces. It's an output from a legacy system loaded into our Oracle database.
If the field has three or more numerals it should have a decimal at three spaces right.
If the value has less than three numerals, it should have a decimal and a leading zero.
For example:
2050 should be converted to 2.050
110 should be converted to .110
50 should be converted to .050
I've tried using cast, but I received the error 'invalid datatype.'
It's a basic select statement:
select
customer_id
cast(ENDING_BALANCE as (decimal(10,3)) as Bal_1
from Current_Balances
Any suggestions would be appreciated.
I think you need to cast it to a number and divide by 1000
SELECT CAST(CAST('2050' as INT)/1000 as DECIMAL(10,3)) FROM DUAL
If you really mean to have the output format looking like that, you need to TO_CHAR it
SELECT LTRIM(TO_CHAR(CAST('2050' as INT)/1000, 'FM0.000'), '0') FROM DUAL

How to round off to 4 decimal places in SQL in such a way that zero gets appended if we have less that 4 digits after the coma

4.145 should be 4.1450, and 4.124489 be 4.1245, I'm using SQL Server 2012
A trailing zero can only exist in a string representation of a numerical value. In SQL Server you can use FORMAT():
SELECT FORMAT(4.145, '0.0000')
See SQLFiddle
Do it like this:
declare #num as float;
set #num=5.20;
select convert(decimal(10, 2), #num);
Or instead search this:
Float datatype with 2 digits after decimal point
Try to understand this one

How to add Leading Zeroes and Decimal Points at the same time in SQL?

I have tried many combinations of the SQL functions; so as to have a 12 digit number including the dot character, including leading zeroes and decimal points.
For example:
for the number 121.22, I want to format it to 000000121.22
or for the number 12.2, I want to format it to 000000012.20
or for the number 100, I want to format it to 000000100.00
I have used the following function; but I lost the decimal points if it's zero.
SELECT RIGHT('000000000000'+ STR(CONVERT(VARCHAR,MYNUMBER),12,2),12);
Any idea on how to solve this problem in Microsoft SQL?
If you're on SQL Server 2012 or later, you can use the format() function.
SELECT FORMAT(121.22, '000000000000.00')
SELECT FORMAT(12.2, '000000000000.00')
000000000121.22
000000000012.20
for ms sql versions not in (2012,2014):
cast(right('000000000',9-len(floor(the_number))) as varchar)
+ cast( cast(the_number as decimal(10,2))as varchar)
for ms sql versions in (2012,2014):
format(the_number ,'000000000000.00')
SELECT padded_id = REPLACE(STR(id, 12), SPACE(1), '0')
Is what I add to use (In SQL server) leading 0's as needed, change the 12 to whatever total number of digits you want it to be.
This allows for non hard coded values, just make sure id or whatever column/param you want to format is set.

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.