In my tables I have columns that is decimal (16,5). When I do
SELECT WorkHours, BillHours
FROM Hours
It gives me be that result:
2.00000 1.00000
2.00500 1.05000
How I can get back this numbers in nice looking format:
2 1
2.005 1.05
You can use ROUND() function in SQL SERVER.
However, all values in one column will have same number of decimal places.
If you want to format it further, consider client side formatting instead of writing custom server side code.
Related
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)
I have 2 columns which I need to divide sum(cola)/sum(ColB), but I am not getting the desired results since SQL server seems to truncate values after decimal
For eg. I have-
select 281370/1035
is giving 271 using simple division, whereas actual result of division is 271.8550724637681 and I want to display 271.8
I tried
SELECT cast(round(281370/1035,1) as numeric(36,1))
but that results 271.0
In SQL Server, you have to cast the integers to decimal and you could use Round to get desired precision.
SELECT cast(Round(CAST(281370 AS decimal) / CAST(1035 AS decimal),1,1) as decimal(10,1))
The problem is that you given the int number and want a decimal result
try this
select convert(decimal(30,10),281370.0/1035.0)
or
select Round(convert(decimal(30,10),281370.0/1035.0),1,1)
#Stormcloak gives the answer to specifically wanting a single position as a mantissa, however to return an exact answer you could "simply" implicitly change the datatype.
select 281370.0/1035
Returns:
271.855072
In Presto DB:
select (CAST(11 as decimal(8,6))/CAST(7 as decimal(8,6))) as result
result:1.571429
decimal(xp,xs)
xp--> total number of digits(before decimal point+ after decimal
point)
xs--> number of digits after the decimal point
reference: https://prestodb.io/docs/current/functions/decimal.html
I am having some number in DB table column, i have written stored procedure to format that number. The requirement is i need to display the number with comma and decimal points. I have tried below, but only comma is coming.. any solutions,
PARSENAME(CONVERT(VARCHAR,CAST(TotalAmount/1000 as MONEY),1),2)
Actual result: 1,134
Expected result: 1,134.0
I want to do this in SQL server itself.
You can accomplish currency formatting as:
SELECT CONVERT(varchar, CAST(5555 AS money), 1)
but i would suggest doing such formatting on the front end, not on the data side
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.
I have in my select clause:
AVG (cast(scale as decimal(5,2)))
I keep getting a number like: 0.6523412897, nothing I do seems to get me to my desired: 0.65.
Basically I want to have a scale of 2 (two decimal places).
Thanks
Cast the average, don't average the cast:
cast(AVG(scale) as decimal(5,2))
Update
Using ROUND() changes the value not the type. Eg. select round(0.12345,2) returns 0.12000 as it should becuase it rounds 0.12345 to 0.12 but keeps the original type with 5 decimals after the point, hence 0.12000. To change the type one must use cast, as in my post.
Try
ROUND(AVG(scale), 2)
It depends upon the database you are using, but ROUND is the answer for SQL Server:
ROUND