How to give select digit after decimal point? - sql

In database there is column amount which datatype is money. I want to select that row only with two digit after decimal. for this how to write the query?
My query is like this:
SELECT AMOUNT FROM DETAIL_PAGE.
I want to modify this query so that it selects two digits after decimal point.

SELECT AMOUNT - FLOOR(AMOUNT) FROM DETAIL_PAGE
That will get you just the decimal though. I think you want
SELECT FORMAT(AMOUNT, 2) FROM DETAIL_PAGE
Or without commas:
SELECT REPLACE(FORMAT(AMOUNT, 2), ',', '') FROM DETAIL_PAGE

Not sure if this is SQL standard and works elsewhere, but in Oracle you can say
select round(amount,2) from detail_page
-- round(12.345, 2) would return 12.35
or
select trunc(amount,2) from detail_page
-- trunc(12.345, 2) would return 12.34

Related

How to round multiple numeric columns to 2 digits in sql?

Say if I need to round multiple numbers to 2 digits now, but I don't want to repeat using round(..., 2) or format(...).
Is there any method to set up the float numbers with 2 digits globally?
select cast(float_column as decimal(10,2))
from your_table
Declare the column or variable of type numeric(18, 2)
You can also use with the CONVERT function
select CONVERT(numeric(18, 2) , 5.54722)

SQL to convert a string to decimal where the last field is a negative identifier

Could anyone help me with converting a string to a positive/negative decimal where the negative indicator is the last value in the field. For example, i have '000000012-' and '000001902 '. I need the first value to return -0.12 and the second to return 19.02, when i sum them i should get a sum of 18.90.
At this point i only know how to get the positive values to work using...
SELECT decimal(sum(decimal(mycolumn,10,2)/100),10,2) as Balance
FROM mytable;
In Standard SQL, you can do something like this:
select sum(case when mycolumn like '%-'
then - cast(replace(mycolumn, '-', '') as decimal(10, 2)) / 100.0
else cast(mycolumn as decimal(10, 2)) / 100.0
end)
This should work in most databases.
I was able to get this to work - was over complicating it. Answer below...
SELECT sum(substr(mycolumn,10,1)||decimal(decimal(substr(mycolumn,1,9),9,2)/100,9,2))
FROM mytable;

How to format % and in 2 decimal points?

How do I code format the return data in 2 decimals and with percentage format like 100.00% or 67.39% instead of 100.000000 or 67.391304?
SUM(qa.scripting1+qa.conduct1+qa.conduct2+qa.conduct3)*100.0/46 as 'C%'
I tried ROUND() but I got the error stating that the round function requires 2 to 3 arguments?
ROUND(SUM(qa.scripting1+qa.conduct1+qa.conduct2+qa.conduct3)*100.0/46) as 'C%'
Thanks!
You can convert to a decimal your original value:
CONVERT(VARCHAR(20), CONVERT(DECIMAL(18,2), SUM(qa.scripting1+qa.conduct1+qa.conduct2+qa.conduct3)*100.0/46) ) + '%' as 'C%'
The first number in the decimal represents the number of digits in the number including decimal places, and the second number represents the number of decimal places.
You should pass number of decimals in second parameter to round function. For formating you can cast number to money and then cast to varchar:
select cast(cast(ROUND(SUM(123.12321)*100.0/46, 2) as money) as varchar) + '%'
Using Round and Cast will work. First round to 2 decimal places then convert to a decimal with 2 places to truncate the excess zeros.
select cast(Round(yourValue, 2) as decimal(18,2))
Sql Fiddle
You can use Format function
select FORMAT(100.0000, 'N' , 'en-us')
returns 100.00
and
select FORMAT(67.391304, 'N' , 'en-us')
returns 67.39
EDIT
In version below 2012 you can do this
SELECT CAST(67.391304 AS NUMERIC(10, 2))
returns 67.39
You can just do:
select FORMAT(0.391304, '##0.00%')
But keep in mind that it implicitly multiplies by 100, so the above will display as 39.13%.

control in display Decimal separators in sql server

I have problem in display the decimal numbers, it has many number.
My sql statement :
Select sum(HS$totalMoney)
the result :
12132.123444343
I want to display as 12132.12 without the another number
Thanks.
If your logic is for money you should first round the values not truncate
select CONVERT(decimal(18,2),round(12132.123444343 ,2)) gives 12132.12
select CONVERT(decimal(18,2),round(12132.125944343 ,2)) gives 12132.13
Try this one -
SELECT CAST(12132.123444343 AS DECIMAL(10,2))
if you are using mysql, use code blew
SELECT TRUNCATE(sum(HS$totalMoney), 2);
this query slove your problem
SELECT CAST(12132.123444343 AS DECIMAL(10,2))
or you can use
select CONVERT(decimal(18,2),round(12132.1255555 ,2))
SELECT CONVERT(decimal(21, 2), sum(HS$totalMoney))
-- This one will round in SQL Server but truncate in ASE 15 (which was available to me at the time)
SELECT CONVERT(decimal(21, 2), round(sum(HS$totalMoney), 2, 1))
-- This one uses a variant of ROUND supported by SQL Server, but not ASE 15 (and will truncate the third and subsequent decimal places).
The round function has a function parameter to truncate instead of round:
select round(12132.123444343 , 2, 1)
From here:
http://msdn.microsoft.com/en-us/library/ms175003.aspx

Formatting an SQL numeric query result with an arbitrary number of decimal places

I have a database table with these two columns:
Amount: numeric (18,0)
DecimalPlaces: numeric (18,0)
This table can store amounts in various currencies, with the decimal place removed from the amount (I can't change this data model). For example, there might be two rows like this:
1290, 2 (This is £12.90, needs to appear as "12.90")
3400, 0 (This is 3400 Japanese Yen, needs to appear as "3400")
I need an SQL query for both Oracle and SQL Server that will format each amount with the correct number of decimal places, preserving any trailing zeroes as illustrated above. I can't use stored procedures, a reporting tool, or Excel.
Your problem is that there isn't an easy way to do this for both SQLServer and Oracle in one query.
The Correct way to do this for SQLServer is to use STR:
Select STR(Amount, 18, DecimalPlaces) from myTable;
The correct way to do this for Oracle is using to_char:
SELECT to_char (amount, '99999999999999.'||rpad('',DecimalPlaces, '0'))
from MyTable;
The queries presented by jms and Andrew won't work in an Oracle query because Oracle SQL uses LENGTH() not LEN(). And Oracle uses to_char() not Cast().
The best I've been able to come up with so far is:
select Amount/power(10, DecimalPlaces) from MyTable
But it doesn't do exactly what I want:
Oracle: the trailing zeroes are stripped, so US$15.00 looks like "15", not "15.00"
SQL Server: a whole lot of extra trailing zeroes are added, so $23.99 looks like "23.99000000000" instead of "23.99"
How about?
select 12345 amount, 2 decimalPlaces, substr( to_char( 12345 ), 1, length (to_char( 12345 ) ) - 2 )
|| '.' || substr( to_char( 12345 ), -2 ) result from dual
/
amount decimalPlaces result
---------- ------------- ------
12345 2 123.45
This is gross but worked for the current inputs on SQL server.
select
substring(
CAST(
CAST(
(amount * power(-0.100000000000000000,decimalPlaces*1.000000000000000000)) as numeric(36,18)
)as varchar(30)
)
,1,len(cast(amount as varchar(20))) + (CASE WHEN decimalPlaces = 0 THEN 0 ELSE 1 END ))
from
myTable
In SQL server you can :
select stuff(convert(varchar,amount) ,
len(convert(varchar,amount)) - DecimalPlaces - 1, 0, ".")
Martlark's answer for Oracle led me to this solution for SQL Server:
select
left(cast(Amount as varchar), len(cast(Amount as varchar)) - DecimalPlaces) +
left('.', DecimalPlaces) +
right(cast(OriginalCurrencyAmount as varchar), DecimalPlaces
) as FormattedAmount
from MyTable