how to get rid off decimals from sql results - sql

I am calculating total hours/minutes but i would like to get rid off the decimals and only show something like this 2.00 hours or 2.5 hours etc. I am getting now something like this: 2.000000 and want only to limit to 2 decimals only.
select DATEDIFF(minute, Min(FullDatetime), Max(FullDatetime)) / 60.0 as hours
from myTable
where userid = 123

You can do it by rounding but the easiest is to format for output using FORMAT().
select FORMAT(DATEDIFF(minute, Min(FullDatetime), Max(FullDatetime)) / 60.0, 'N2') as hours
from myTable
where userid = 123
Helpful original documentation: here

try use
cast('values' as decimal(18,2)) --2 decimal place.
select Cast((DATEDIFF(minute, Min(FullDatetime), Max(FullDatetime)) / 60.0 as hours)as decimal(18,2))
from myTable
where userid = 123

There are a few options out there.
I prefer to use the following when no rounding is needed
FORMAT(value, 'N2')
SQL - Rounding off to 2 decimal places
how to get 2 digits after decimal point in tsql?

just use ROUND function such as : SELECT ROUND(columnName,decimals) from table

You could use STR or CONVERT function:
DECLARE #v NUMERIC(19, 6)
SET #v = 2.189189
SELECT STR(#v, 19, 2) AS Method1_Result_VARCHAR, CONVERT(NUMERIC(15, 2), #v) AS Method2_Result_NUMERIC
/*
Method1_Result_VARCHAR Method2_Result_NUMERIC
---------------------- ----------------------
2.19 2.19
*/
Note: First argument of STR function has float type and this means that 1) SQL Server will convert this argument from numeric to float and 2) method 1 uses a non-deterministic expression.

Related

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.

DATEDIFF() to just return age with 2 decimal points

I have a SELECT statement requesting the age of he individual when a test was made:
SELECT
DATEDIFF(dd,BIRTH_DATE,TEST_DATE)/365.25 [Age at Result]
FROM TABLE
WHERE ID = '100'
The result comes out like 2.056125.
I saw on another post to convert to seconds and divide by 86400.0, but I was still getting 6 decimal points.
What I was looking back was to get the age as 2.05 or even round to 2.00.
Thanks
You can cast to a decimal with the precision you want:
SELECT CAST(DATEDIFF(day, BIRTH_DATE, TEST_DATE)/365.25 as DECIMAL(10, 2)) as [Age at Result]
FROM TABLE
WHERE ID = 100;
Note: I removed the single quotes around "100". Only use single quotes if it the id is a string.
I would use the following -
DECLARE #TestDate DATETIME = GETDATE()
DECLARE #BirthDate DATETIME = '1995-06-06 08:00:00.000'
SELECT CAST(DATEDIFF(dd,#BirthDate,#TestDate)/365.25 AS DECIMAL(10, 2)) [Age at Result]

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))

SQL decimal truncation not rounding

Here is the sql i wrote but looking for 5 digits after decimal without rounding.
Select convert(decimal(10,5),Cast(473 as float) / Cast(14322 as float)) --i get 0.03303
Select Cast(473 as float) / Cast(14322 as float) --i get 0.033026113671275
But i'm looking for 0.03302.
Five digits after decimal which (03302) with out rounding.
The first query is rounding it off to 0.3303 instead of 0.03302
Let me know.
Use ROUND with the optional third parameter to truncate:
Select convert(decimal(10,5),
ROUND(Cast(473 as float) / Cast(14322 as float)
,5,1 // the 1 tells SQL to truncate instead of round
))
SELECT CONVERT(DECIMAL(10,5), 473 / (14322 * 1.0) - 0.000005);