Formatting INTERVAL DAY(0) TO SECOND(0) - sql

I have a few time fields that are INTERVAL DAY(0) TO SECOND(0) Datatypes to store various times of the day.
Currently on normal retrieval, my fields display as such:
+00 19:00:00.000000
How am I able to format this such that it displays as 19:00?

You want hours and minutes from an interval. If that interval cannot exceed 24 hours, then you can add the interval to a date without time part and then use TO_CHAR to get the formatted hours and minutes:
select to_char(trunc(sysdate) + myinterval, 'hh24:mi') from mytable
If, however, the interval can be larger (e.g. 100 hours and 15 minutes: 100:15), then you'll need another approach:
select
to_char(extract (day from myinterval) * 24 + extract (hour from myinterval)) ||
':' ||
to_char(extract (minute from myinterval), 'fm00')
from mytable

In contradiction to documentation you cannot use function TO_CHAR() for intervals. It allows to use INTERVAL values but the result is always the same. On another place the documentation says:
Interval data types do not have format models.
You can use either EXTRACT() function or you can work with Regular Expressions:
WITH t AS
(SELECT INTERVAL '19' HOUR + INTERVAL '30' MINUTE AS i FROM dual)
SELECT
LPAD(EXTRACT(HOUR FROM i), 2, '0')||':'||LPAD(EXTRACT(MINUTE FROM i), 2, '0'),
REGEXP_SUBSTR(i, '\d{2}:\d{2}')
FROM t;
Usually it is not very smart to use Regular Expression on date/time values. On the other hand, since format of INTERVAL does not depend on any NLS-Settings it is a feasible option.
Disadvantage of EXTRACT is when you have negative intervals. Such an interval would be written like -19:-30 because every component is returned with minus sign (-). Also be aware that EXTRACT returns NUMBER values, so you have to pad them with '0' manually.

Use
substr(to_char(interval_value), 5, 5)
where interval_value is the column name (incidentally, tables in a relational database don't have fields; they do have columns).

Related

Convert minutes to Hours:Minutes in Oracle SQL

Morning Team,
I have an Oracle SQL script that is calculating from creation of an event and how many minutes old compared to the systimestamp.
I need to convert the minutes, which are coming out as 120 for 2 hour for example, into Hours:Minutes version, i.e. 2:00
I'm struggling with that part and would like to ask if someone could help? My current code for the calculation is:
(ROUND(((cast(systimestamp as date) - cast(n.createdttm as date)))*1440,0)) "Minutes old",
I'm sure it's something simple but with all my fiddling I am not able to get it.
Thank you
You can create an INTERVAL form the minutes.
select numtodsinterval(120, 'minute') from dual
Or create a datetime from the minutes and convert its time part to a string of hours and minutes.
select to_char(trunc(sysdate) + interval '1' minute * 120, 'hh24:mi') from dual
It looks like createdttm is a timestamp, so you can just subtract:
systimestamp - createdtm
... to get an interval value like +000000000 02:00:00.00000. You can't format that directly, but you can either extract the various elements and concatenate those back together, or treat it as a string and cut out the bits you want.
If you only want the time part and it will always be less than a day you can just do:
substr(systimestamp - createdtm, 12, 5)
02:00
But if it can go over 24 hours then you probably want the day part too, which you could still get just with substr (and maybe replace to change the space to another colon) if you know it can never be more than 2 days:
substr(systimestamp - createdtm, 10, 7)
0 02:00
That's unlikely to be a safe assumption though, so instead you could extract the number of days and concatenate that:
extract(day from (systimestamp - createdtm)) || ':' || substr(systimestamp - createdtm, 12, 5)
0:02:00
You could only show the number of days if it's non-zero, but that would probably be quite confusing to who/whatever is looking at the results; but if you really wanted to:
case when extract(day from (systimestamp - createdtm)) > 0
then extract(day from (systimestamp - createdtm)) || ':'
end || substr(systimestamp - createdtm, 12, 5)
02:00
db<>fiddle with a few sample values.
One thing to note is this effectively truncates the seconds off the time; your original attempt included round(), but that might not have been what you meant.
If u want to convert minutes to hours and minutes.
If x is the number of minutes (such as 350):
TO_CHAR ( FLOOR (x / 60)) || ':'
|| TO_CHAR ( MOD (x, 60)
, 'FM00'
)
If u want to convert hours and minutes to minutes.
SELECT (TRUNC (x) * 60) +
( (MOD (x, 1)
* 100
)
FROM dual;
where x is a NUMBER. If you have a sting, s, instead, use TO_NUMBER (s) in place of x.

Interval Date to days [duplicate]

I have two timestamp columns: arrTime and depTime.
I need to find the number of munites the bus is late.
I tried the following:
SELECT RouteDate, round((arrTime-depTime)*1440,2) time_difference
FROM ...
I get the following error: inconsistent datatype . expected number but got interval day to second
How can i parse the nuber of minutes?
If i simply subtract: SELECT RouteDate, arrTime-depTime)*1440 time_difference
The result is correct but not well formatted:
time_difference
+00000000 00:01:00 0000000
The result of timestamp arithmetic is an INTERVAL datatype. You have an INTERVAL DAY TO SECOND there...
If you want the number of minutes one way would be to use EXTRACT(), for instance:
select extract( minute from interval_difference )
+ extract( hour from interval_difference ) * 60
+ extract( day from interval_difference ) * 60 * 24
from ( select systimestamp - (systimestamp - 1) as interval_difference
from dual )
Alternatively you can use a trick with dates:
select sysdate + (interval_difference * 1440) - sysdate
from (select systimestamp - (systimestamp - 1) as interval_difference
from dual )
The "trick" version works because of the operator order of precedence and the differences between date and timestamp arithmetic.
Initially the operation looks like this:
date + ( interval * number ) - date
As mentioned in the documentation:
Oracle evaluates expressions inside parentheses before evaluating those outside.
So, the first operation performed it to multiply the interval by 1,440. An interval, i.e. a discrete period of time, multiplied by a number is another discrete period of time, see the documentation on datetime and interval arithmetic. So, the result of this operation is an interval, leaving us with:
date + interval - date
The plus operator takes precedence over the minus here. The reason for this could be that an interval minus a date is an invalid operation, but the documentation also implies that this is the case (doesn't come out and say it). So, the first operation performed is date + interval. A date plus an interval is a date. Leaving just
date - date
As per the documentation, this results in an integer representing the number of days. However, you multiplied the original interval by 1,440, so this now represented 1,440 times the amount of days it otherwise would have. You're then left with the number of seconds.
It's worth noting that:
When interval calculations return a datetime value, the result must be an actual datetime value or the database returns an error. For example, the next two statements return errors:
The "trick" method will fail, rarely but it will still fail. As ever it's best to do it properly.
SELECT (arrTime - depTime) * 1440 time_difference
FROM Schedule
WHERE ...
That will get you the time difference in minutes. Of course, you can do any rounding that you might need to to get whole minutes....
Casting to DATE first returns the difference as a number, at least with the version of Oracle I tried.
round((cast(arrTime as date) - cast(depTime as date))*1440)
You could use TO_CHAR then convert back to a number. I have never tested the performance compared to EXTRACT, but the statement works with two dates instead of an interval which fit my needs.
Seconds:
(to_char(arrTime,'J')-to_char(depTime,'J'))*86400+(to_char(arrTime,'SSSSS')-to_char(depTime,'SSSSS'))
Minutes:
round((to_char(arrTime,'J')-to_char(depTime,'J'))*1440+(to_char(arrTime,'SSSSS')-to_char(depTime,'SSSSS'))/60)
J is julian day and SSSSS is seconds in day. Together they give an absolute time in seconds.

SQL Invalid operation for DateTime or Interval when casting a hour/minute to an existing Timestamp

I'm trying to cast an existing Date variable as a timestamp, and add hours and minutes from another Time variable to get a final variable of the format mm/dd/yyyy hh:mm:00.
The current line of the query that errors out is:
cast(DepDt as timestamp) + cast(substr(ArrTm, 1, 2) as interval hour) + cast(substr(ArrTm, 3, 2) as interval minute) as Arrv_DTML
I can't seem to find what's wrong though. I have gotten rid of the substring functions to make sure it wasn't something wrong with that, but I can't seem to cast the ArrTm as an interval even on its own. Is it something with the format of the variables? I am running this in Teradata.
DepDt is a Date. ArrTM is a Time variable.
You can't apply substr on a Time, you must explicitly cast it to a VarChar first:
Cast(DepDt AS TIMESTAMP(0))+
+ Cast(Substr(Cast(ArrTm AS VARCHAR(8)), 1, 5) AS INTERVAL HOUR TO MINUTE)
But there's an easier way to get your result:
Cast(DepDt AS TIMESTAMP(0)) -- date to Timestamp
+ (Extract(HOUR From ArrTm) * INTERVAL '1' HOUR) -- hour to Interval
+ (Extract(MINUTE From ArrTm) * INTERVAL '1' MINUTE) -- minute to Interval

Setting Minute and Second component of Timestamp field to 0

I have a requirement such that if I enter a timestamp(yyyy-mm-dd hh:mm:ss) say
2015-04-05 16:45:12
I need it converted to
2015-04-05 16:00:00
Is there an elegant way to do this in Teradata rather than extracting hour and then casting it with date?
Instead of casting from/to a string two times it's probably more efficient to substract intervals:
ts - (EXTRACT(MINUTE FROM ts) * INTERVAL '01' MINUTE)
- (EXTRACT(SECOND FROM ts) * INTERVAL '01' SECOND)
If this is to much code simply put it in a SQL UDF.
One way is to extract the substr you want to retain the values, and then concatenate the zeros for minutes and seconds.
For example,
SELECT concat(substr('2015-04-05 16:45:12', 1, 14), '00:00');
If you want, you could convert this back to timestamp, thus you will have the new timestamp value with the minutes and seconds value set to zero.

Compare date + time with timestamp

I have a table with two temporal columns. First (name is DATE) is storing the date (not including the time part) and therefor the datatype is DATE. Second column (name is TIME) is for storing the time in seconds and therefor the datatype is NUMBER.
I need to compare this two dates with a timestamp from another table. How can I calculate the date of the two columns (DATE and TIME) and compare to the timestamp of the other table?
I have tried to calculate the hours out of the time column and add it to the date column, but the output seems not correct:
SELECT to_date(date + (time/3600), 'dd-mm-yy hh24:mi:ss') FROM mytable;
The output is just the date, but not the time component.
You can use the INTERVAL DAY TO SECOND type:
SELECT your_date + NUMTODSINTERVAL(your_time_in_seconds, 'SECOND') FROM dual;
Example:
SELECT TRUNC(SYSDATE) + NUMTODSINTERVAL(39687, 'SECOND') FROM dual;
The calculated date with time is: 10-11-2013 11:01:27
This is a better idea than dividing your value by 3600 in my opinion, as you have an interval in seconds, so it feels natural to use an interval to represent your time, which can then be easily added to a column of DATE datatype.
Oracle Interval in Documentation
NUMTODSINTERVAL Function in documentation
date + (time/3600) is already a DATE, so you don't need to do to_date(). It does have the time part you added though, you just aren't displaying it. If you want to output that as a string in the format you've shown, use to_char() instead:
SELECT to_char(date + (time/3600), 'dd-mm-yy hh24:mi:ss') FROM mytable;
... except that if time is actually in seconds, you need to divide by 86400 (24x60x60), not 3600. At the moment you're relying on your client's default date format, probably NLS_DATE_FORMAT, which doesn't include the time portion from what you've said. That doesn't mean the time isn't there, it just isn't displayed.
But that is just for display. Leave it as a date, by just adding the two values, when comparing against you timestamp, e.g.
WHERE date + (time/86400) < systimestamp
Try like this,
SELECT TO_DATE('11/11/2013','dd/mm/yyyy') + 3600/60/60/24 FROM DUAL;
Your query,
SELECT date + time/60/60/24 FROM mytable;
try using to_timestamp instead of to_date