how to convert minutes to days in oracle - sql

How would you convert a number of minutes to number of days? For instance, number of minutes is 20160. Now how do I get number of days based on that using SQL?

--divide by 60 get the number of hours and then by 24 to get the number of days
select 20160/60/24 as days_from_min
from dual

You divide:
select 20160 / (24 * 60) as num_days
This returns a fraction. You can floor() or round() to get a whole number.

Only for the sake of completeness: here is a way to do it with Oracle functions. (For people who do not want to just assume that the Earth will continue to rotate at a constant speed for the life of their software.)
select extract(day from numtodsinterval(20160,'MINUTE')) days
from dual;
This gives full days only, so it's essentially the same as
select floor(20160/(24*60)) days from dual;

Related

How to calculate difference between two days in hours in postgreSQL and return as days?

I want to calculate the difference between two dates in days such that for hours from 1-23 we consider it as a day.
for example: Date1 = '2021-06-15 01:52:00.926+00'
Date2 = '2021-06-15 02:52:00.926+00'
Here, Date1-Date2 = 1hour. I want to take ceil of it to be 24hrs i.e 1 day.
I tried (Date2::DATE - Date1::DATE) but it gives 0.
There can be two scenarios: If difference between days is 35hrs it should return 3(i.e 3 days). If the difference between dates is 5hrs it should return 1(i.e 1 day).
You can use epoch arithmetic:
select ceiling(extract(epoch from date2) - extract(epoch from date1)) / (24 * 60 * 60)
from t;
Note that this particular formulation counts anything longer than 1 day as 2 days. I think that is the intention of your question. However, if you really do have a 1 hour buffer, the logic could be tweaked to handle that.
Not technically an answer to your PostgreSQL question, however in Ingres (Postgres was the successor to Ingres) we have a interval function which returns the interval between two dates. Do you have anything similar in PostgreSQL?
e.g. select interval('hours', date1 - date2)

How to convert a count of days between two dates as numeric in OracleSQL?

I'm using this query
select current_date - table.DOB/365 as checkagainst
to get the days between today's date and a person's date of birth. It is returning what looks like a numeric count of days, but then when I try to divide it by 12, I get the error:
ORA-00932: inconsistent datatypes: expected NUMBER got DATE.
I just want to divide the date difference between a DOB and today's date by 365 to get age in years.
Use parentheses:
select ( current_date - table.DOB )/365
Or, better yet, use months_between():
select months_between(table.DOB, current_date) / 12
Difference between dates returns number of days between two dates.
So use following:
Floor(Sysdate - table.DOB) -- number of days
Floor(Months_between(sysdate, table.DOB)) -- number of months
Floor((Months_between(sysdate, table.DOB)/12) -- number of years
Floor will make sure to show the rounded to the nearest small number as age should be.
Cheers!!

TIMESTAMPDIFF Missing Days

Can someone explain to me why this returns only 360 days and not 365 days?
I expect it to not count the first day but, what about the other 4 days?
SELECT
(TIMESTAMPDIFF(16,CHAR(TIMESTAMP('2017-12-31') - TIMESTAMP('2017-01-01'))))
FROM sysibm.sysdummy1
I am planning on just adding + 5 at the end.
If you have DB2 for Linux, Unix and Windows - now called Db2 - Version 11.1 you could also use
SELECT DAYS_between('2017-12-31','2017-01-01') FROM SYSIBM.SYSDUMMY1
I think the documentation explains this pretty well:
The returned estimate may vary by a number of days. For example, if
the number of days (interval 16) is requested for the difference
between '1997-03-01-00.00.00' and '1997-02-01-00.00.00', the result is
30. This is because the difference between the timestamps is 1 month, and the assumption of 30 days in a month applies.
In other words, the difference is 11 months and 30 days -- 11 * 30 + 30 = 360.
SELECT DAYS(DATE('2017-12-31')) - DAYS(DATE('2017-01-01'))
FROM sysibm.sysdummy1
For a more exact representation, try:

sql statement - would like to subtract 1 date from another and get the days hours and mins inbetween

I would like to subtract 1 date from another and get the days hours and mins in-between.
I know there is a DateDiff function, however it does not work with all 3 time values; days hours and mins. I would like this doable in an SQL statement. Currently I have the following.
SELECT id, pickupdateandtime, GETDATE() AS CurrentTime,
(DATEDIFF(day,GETDATE(),pickupdateandtime)) AS Days,
(DATEDIFF(hour,GETDATE(),pickupdateandtime)) AS Hours,
(DATEDIFF(minute,GETDATE(),pickupdateandtime)) AS Mins FROM orders
And it shows up like this:
If we can stick it all in 1 column that's fine too.
I agree with #AndyMcLaughlin about the use of the mod operator % here. It's very handy for this sort of thing. However, I have a general distrust of DATEDIFF. That function does not count the whole number of years (say) between two dates, but the number of year boundaries between them.
So DATEDIFF "thinks" the difference in years between 01-Jan-2000 and 01-Jan-2001 is the same as that between 31-Dec-2000 and 01-Jan-2001.
This is why #Michael saw a need to subtract 1 from #AndyMcLaughlin's results. Unfortunately, that doesn't always work, it will depend on the individual case.
As a rule, DATEDIFF works well when it's used against the smallest interval you are interested in. So if you are interested in years and simply want to separate one calendar year from another, it'll serve you well.
I think the smallest interval we are interested in here is minutes. So we can use DATEDIFF for that, but have to work upwards from there to hours and days:
select
mf.id,
mf.pickupdateandtime,
mf.CurrentTime,
--The divisions in the following lines simply
--truncate since all the numbers are integers
--but that works in our favour here
(mf.MinutesFull/(60*24)) as Days,
(mf.MinutesFull/60) % 24 as Hours,
mf.MinutesFull % 60 as Minutes
from
(
select
id,
pickupdateandtime,
getdate() as CurrentTime,
datediff(minute, getdate(), pickupdateandtime) as MinutesFull
from #orders
) mf
You need to use the mod operator % to remove whole days from hours and whole hours from minutes.
So you can do something like:
SELECT
id,
pickupdateandtime,
GETDATE() AS CurrentTime,
(DATEDIFF(day,GETDATE(),pickupdateandtime)) AS Days,
(DATEDIFF(hour,GETDATE(),pickupdateandtime) % 24) AS Hours,
(DATEDIFF(minute,GETDATE(),pickupdateandtime) % 60) AS Mins FROM orders

Cannot convert number to date

I have problem converting number column to date, I did the following
SELECT to_date('12-30-1899 1:00:00','MM-DD-YYYY HH24:Mi:SS') + (createDate/1440)
FROM table_A;
and got the query result
10/17/5826 17:18
The month and date including hours and seconds is right but the year is different I got 5826. Its also the same for the other rows i got different results for year. I did follow some examples on this here. But still got wrong result. Can anyone help on this thanks.
The samples below are createDate column values:
1300844909778
1302831103113
1303210978316
1396963615616
Date arithmetic in Oracle assumes days. As it stands you are dividing a very large number by 1440 and adding that number of days to your starting date. That's why you're getting results in the far future.
So what value does createdate represent? It's clearly not an actual date. Your choice of 1440 as denominator suggests you think it's meant to be "number of minutes" but if the dates are so far out of expectation that is not it either.
I thought could be values represented in the Unix epoch because the numbers start with 13. Except that they're way too big. Current Unix timestamps should be ten digits. You've got thirteen digits.
Could they be Unix epoch plus milliseconds?
I have created a SQLfiddle to test this theory. Treating the first ten digits of your createdate values as seconds and adding that number to the Unix date produces sensible dates. Check it out.
So the theory holds water. But I doesn't help with your query. Adding two dates together doesn't make any sense. What are you actually trying to achieve? If your're looking for an interval you need to subtract the earlier date from the later one.
The createDate could be the number of milliseconds. It is just a guess. If so, then maybe this helps:
SELECT to_date('12-30-1899 1:00:00','MM-DD-YYYY HH24:Mi:SS') + (1300844909778/(1000*60*60*24))
FROM dual
/
3/21/1941 2:48:30 AM