SQL Casting with Decimals - sql

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

Related

How to Cast values to decimal in Hive

This is example of the column "Amount" i am working on. I need to remove all '+' and '-' and all leading zeros, then convert to decimal. There are also cases when there is only zeros present and i am not sure what is the best approach for it. Is there a better solution? Please help on this.
Amount
-0000211
+0000101
+0000000
+0000013
CAST(CAST(CAST(Amount as INT) AS varchar(8)) as decimal(8,2)) from payment_table;
use abs() to remove +/- and 0, and then use cast() to cast to decimal.
select CAST( ABS(Amount) as DECIMAL(8,2)) as ret_Amount -- this will return 211.00

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.

How to convert Time to Decimal in SQL with an integer

I am having a couple of issues converting time to decimal...
Essentially I have used a DATEDIFF to identify the hours and minutes between two dates and times. This produces a result of 5.45, however I would like to get the result as 5.75.
select RTRIM(DATEDIFF(second, startdate, enddate)/3600)+'.'
+ RIGHT('0'+RTRIM((DATEDIFF(second, startdate, enddate) % 3600)/60),2)
from time
I have tried a few things but I believe the issue is that this is not an integer and that's why i cant convert the minutes.
Get the time difference in minutes, and divide by 60, this will place hours before the decimal separator and minutes after:
datediff(minute, startdate, enddate) / 60.0
Select DATEDIFF(second, startdate, enddate)/3600.0
As mentioned in this answer, "Take the DateDiff in seconds instead, and then divide by 86400.0. The decimal point is required."

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

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

TSQL DateDiff to return number of days with 2 decimal places

I need to compare 2 dates and return the number of days in between with 2 decimal places.
For example:
when comparing
SubmittedDate = 2012-02-29 07:02:55.000
FirstCall = 2012-02-29 12:12:19.000
That is a 5 hour difference. So I would return 0.2 days
I have tried:
CAST(DATEDIFF(Hour, SubmittedDate, FirstCall)/30.0 AS DECIMAL(5,2)) As HoursBeforeFirstCall
CAST(DATEDIFF(Day, SubmittedDate, FirstCall) AS DECIMAL(5,2)) As HoursBeforeFirstCall
None seem to work.
Take the DateDiff in seconds instead, and then divide by 86400.0. The decimal point is required.
When you represent a date as a number, each day is represented by 1.0 "units" already. To get the timespan of two dates as a decimal, you can just subtract them.
SELECT CAST((#FirstCall - #SubmittedDate) AS NUMERIC(10, 2))
Here is what I've done:
ROUND(SUM(DATEDIFF(ss,StartDateTime,EndDateTime) / 60.0 / 60.0), 2)
How about this.
select convert(decimal(12,2),convert(decimal(12,2),datediff(second,'2012-02-29 07:02:55.000','2012-02-29 12:12:19.000'))/60/60/24)