SQL number decimals - sql

I run a query and all my numbers are coming out to 5 decimal points:
for example -
156713.55000
2103613.03000
2080.08000
is there a simple piece of code I can add into my code so the 'Cost' table results are to 2 decimal points?

Following example will help you.
With rounding:
select ROUND(55.4567, 2, 0)
-- Returns 55.4600
select CAST(55.4567 as decimal(38, 2))
-- Returns 55.46
Without rounding:
select ROUND(55.4567, 2, 1)
-- Returns 55.4500
select CAST(ROUND(55.4567, 2, 1) as decimal(38, 2))
-- Returns 55.45
or
Use Str() Function. It takes three arguments(the number, the number total characters to display, and the number of decimal places to display
Select Str(12345.6789, 12, 3)
displays: ' 12345.679' ( 3 spaces, 5 digits 12345, a decimal point, and three decimal digits (679). - it rounds if it has to truncate
for a Total of 12 characters, with 3 to the right of decimal point.

Just use the ROUND function:
SELECT ROUND(column, 2) FROM Cost
Or to strip the decimals and round, use CAST:
SELECT CAST(column as decimal(10, 2))

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)

How do I convert a 5 or 6 digit decimal to a date in sql

I've got a column that shows the date as a decimal such as 101118 for 10-11-18 and 90118 for 09-01-18. I am trying to create a simple report that would give me all reservations yesterday.
So for example
Select playerid, datereservationmade
from dbo.lms
normally there is very simple and I would just do
Select playerid, datereservationmade
from dbo.lms
where datereservationmade >= dateadd(day,datediff(day,1,GETDATE()),0)
AND datereservationmade < dateadd(day,datediff(day,0,GETDATE()),0)
That does not work in this case because the datereservationmade field is a decimal and if its a month 1-9 it leaves off the 0 and makes it a 5 digit decimal then if its 10-12 it is a 6 digit decimal.
Someone please help me figure out how to convert this!
If at all possible, you really should fix your schema so that dates are actually being stored as dates.
If you need to work with the decimal data type, you can use something like the following...
IF OBJECT_ID('tempdb..#TestData', 'U') IS NOT NULL
BEGIN DROP TABLE #TestData; END;
CREATE TABLE #TestData (
decimal_date DECIMAL(6, 0) NOT NULL
);
INSERT #TestData (decimal_date) VALUES (101118), (90118), (101718);
--==============================================
SELECT
td.decimal_date,
dd.date_date
FROM
#TestData td
CROSS APPLY ( VALUES (RIGHT('0' + CONVERT(VARCHAR(6), td.decimal_date), 6)) ) cd (char_date)
CROSS APPLY ( VALUES (CONVERT(DATE, STUFF(STUFF(cd.char_date, 5, 0, '/'), 3, 0, '/'), 1)) ) dd (date_date)
WHERE
dd.date_date = CONVERT(DATE, DATEADD(DAY, -1, GETDATE()));
Convert the decimal to varchar(6) by adding a zero in front and getting the RIGHT 6 characters.
Then convert the string to a date from its parts, which are substrings in your varchar(6). This is made easier in SQL Server 2012 with the DATEFROMPARTS function.
Using the DATEFROMPARTS, as Tab Alleman suggested, you might get something like this:
-- Example of the arithmetic
SELECT 101118 / 10000 AS Month, (101118 % 10000) / 100 AS Day, (101118 % 100) AS Year
-- Using the math in DATEFROMPARTS
SELECT DATEFROMPARTS((101118 % 100) + 2000, 101118 / 10000, (101118 % 10000) / 100 )
However, I'm skeptical that you've provided all the correct information. What happens on January first? Your decimal value won't start with zero (as you stated). Will your day always pad with zero? If not, then 1119 won't produce the same result as 10119. If, however, your day does start with zero, then the equation above should work fine.

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%.

How do i convert minutes to represent hours decimally in sql?

I have Minutes 140. Which in hours and minutes becomes 2:20.
In this case i would love to get 2.33.
What i've tried:
select cast(140/60 as varchar(8)) + '.' + cast((140 % 60) as varchar(8))
Outputs: 2.20
select 140/60
Outputs: 2
select cast(140/60 as decimal(5,2))
Outputs: 2.00
What am i missing?
How do i convert 140 minutes to represent hours decimally?
Sorry about the quick comment, i'll try to explain a little more clearly about this.
By default, Sql will "think" that both your dividend & divisor are INT data type, that's why it returns 2.
If you specify the number with decimal, like this :
select (140.00/60.00)
now the data type is not int any more, and the result is : 2.3333333
So, you will need to convert one of the data type to float, decimal, numeric(n, n) to get the accurate result :
select cast(140 as decimal(5, 2)) / cast(60 as decimal(5, 2))
But you still can just convert only dividend or divisor, like this :
select 140 / cast(60 as decimal(5, 2))
or
select cast(140 as decimal(5, 2)) / 60
they both gave the same result, becasue the result type is the data type of the argument with the higher precedence, in this case, decimal has the higher precedence than int
you can read more here :
Divide
Data Type Precedence
Try this
Select convert(decimal(5,2),convert(float,140)/60)
or
Select cast(140.00/60 as decimal(5,2))

How to give select digit after decimal point?

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