SQL rounding to 7 places, even though its set as decimal(18,4)) - sql-server-2005

select (cast(datediff(minute,'1900-01-01 07:03:00.000' ,'1900-01-01 10:35:00.000')
as decimal(18,4))/60)
Even though the decimal places are set to 4, it returns
3.5333333
instead of
3.5333
Please help me return 3.5333 (4 decimal places)
http://sqlfiddle.com/#!3/1fa93/4119/0

Put the /60 in the cast and change it to 60.0 to have a decimals.
select (cast(datediff(minute,'1900-01-01 07:03:00.000' ,'1900-01-01 10:35:00.000')/60.0
as decimal(18,4)))

You need to do your division before casting:
select
cast(datediff(minute, '1900-01-01 07:03:00.000', '1900-01-01 10:35:00.000') / 60.0
as decimal(18,4))
Then you get:
3.5333
Casting to decimal(18,4) and then doing a division by 60 sort of "invalidates" your formatting - SQL Server will show all the numeric precision of that division by 60 again...

Related

Decimal value to HH.MM conversion problem. 0.33333 Decimal value not showing as 00.20 minutes

I am having an issue converting a decimal such as 0.33333, which needs to show as 20 minutes. Using the formula I have it is showing as 19 minutes. I hope someone can assist please. Thank you. Currently the code I am using example.
declare #value float = 0.3333333
select
Cast(CONVERT(VARCHAR, CONVERT(INT, Floor(cast(#VALUE as Decimal (10,2)))))
+ '.' + CONVERT (VARCHAR, CONVERT(INT, (cast(#value as Decimal (10,2)) - Floor(cast(#VALUE as Decimal (10,2)))) * 60.0)) as Decimal(10,2)) [hh.mm],
cast(#value as money) [DecimalHours]
-- integer part -- fractional part -- final rounding
cast(floor(#value) + ((#value - floor(#value)) * 0.6) as dec(18,2))
Works for more than 24 hours
Probably a lot easier when you use a date/time function:
SELECT FORMAT( DATEADD(n,ROUND(60.0 * #value,0) ,0) , 'HH:mm')

datediff() of 2 timestamps that contain decimal values

I've been stumbling with this issue for a couple days now, and cannot seem to figure out why, when my getdate() insert into the columns are providing a millisecond decimal to the military time format, I still cannot seem to be able to pull a decimal format datediff() result. Does it have to do with the engine not recognizing the decimal due to the surrounding '' characters?
When I use:
select datediff(s,'2013-06-01 21:59:59.141','2013-06-01 23:59:59.997')
It returns:
7200
And when I use:
select cast(datediff(s,'2013-06-01 21:59:59.141','2013-06-01 23:59:59.997') as float);
It returns:
7200
I am at a loss as to what I am missing in order to result in a decimal value.
Thanks
If you are trying to get the milliseconds of the difference, and you want to convert the units to seconds, you can try using something like the following:
SELECT DATEDIFF(MS,'2013-06-01 21:59:59.141','2013-06-01 23:59:59.997') / 1000.0
That'll produce: 7200.856000.
Please note that DATEDIFF(MS, ...) requires guarding for long time spans or it will give an overflow:
SELECT datediff(MS, '2013-06-30 23:59:59.997', '2013-06-01 21:59:59.141')
-- FAILURE: The datediff function resulted in an overflow. The number of dateparts separating two date/time instances is too large
You can use following method which is overflow-safe and gives you a float result:
SELECT cast(cast('2013-06-01 23:59:59.997' as datetime)-cast('2013-06-01 21:59:59.141' as datetime) as float) * 24.0
-- Returned 2.00023796296296
SELECT cast(cast('2013-06-30 23:59:59.997' as datetime)-cast('2013-06-01 21:59:59.141' as datetime) as float) * 24.0
-- Returned 698.000237962963
I'm not sure how this method works when time zone changed during the measured date period.

SQL Casting with Decimals

Lets say I get 8048 seconds for my SQL code:
SUM(DATEDIFF(s, '8/25/2015', '8/26/2015')) as 'Seconds'
How can I get the same answer for Minutes and hours... but have them be a decimal? This is for hours:
Cast(((SUM(DATEDIFF(s, '8/25/2015', '8/26/2015'))/60.00)/60.00) as decimal(8,2)) as 'Hours'
But I keep getting 2.00 hours.... when it should be 2.34 hours?
DATEDIFF returns an interval. CAST it as a float or decimal to get a decimal result from your math formula:
Cast(((SUM(CAST(DATEDIFF(s, '8/25/2015', '8/26/2015') AS decimal(8,2))/60.00)/60.00) as decimal(8,2)) as 'Hours'
Try relocating the CAST statement to inside of the SUM():
CAST((SUM(CAST(DATEDIFF(s, [date1], [date2]) AS Decimal(20,2))/60.00)/60.00)) AS Decimal(20,2)) as 'Hours'
The way you have it set up now, the value is probably rounded before you even cast it

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

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