Getting date time stamp difference minutes in Oracle Database 12c Enterprise Edition - sql

I have this query to get the date time stamp difference minutes in Oracle Database 12c Enterprise Edition Release 12.1.0.1.0 - 64bit Production
SELECT TO_CHAR(GPS_FULL_DATE+2/24, 'MM-DD-YYYY HH24:MI:SS') GPS_DATE,
TO_CHAR(CREATION_DATE, 'MM-DD-YYYY HH24:MI:SS') CREATION_DATE,
extract(minute from ((GPS_FULL_DATE+2/24)-CREATION_DATE)) mins
FROM server_data sd
But I got this error:
ORA-30076: invalid extract field for extract source
30076. 00000 - "invalid extract field for extract source"
*Cause: The extract source does not contain the specified extract field.
*Action:
Error at Line: 2 Column: 42

(GPS_FULL_DATE + 2/24) - CREATION_DATE
returns number of days between two dates. Therefore, you can't extract number of minutes of it - you could, though, multiply the result by 24 * 60 (hours in a day * minutes in an hour) to get the desired value.

Related

ORA-00932 convert and format dates and strings to calculate a range of days

I have a table where the date_one field is of type VARCHAR2 (20 BYTE) and the date_two field is of type DATE.
Output for date_one is 29-01-2019 20:22:08 and output for date_two is 25-JAN-19.
I need to calculate records based on an interval of days between these two fields but I'm having a hard time with this error:
select field1,field2,field3
from mytable
where
trunc(to_date(DATE_ONE, 'DD-MM-YYYY HH24:MI:SS')) - trunc(to_date(DATE_TWO 'DD-MM-YYYY')) > interval '15' day;
ORA-00932: inconsistent datatypes: expected NUMBER, got INTERVAL DAY
TO SECOND
00932. 00000 - "inconsistent datatypes: expected %s got %s"
How can I fix this error?
The result of subtracting two dates is a number of days, unlike the result of subtracting two timestamps, which is an interval. You are currently trying to compare the number you get from that subtraction with a fixed interval - which is what the error is saying. The data types don't match and can't be implicitly converted for comparison.
You can either convert that number to an interval:
select field1,field2,field3
from mytable
where (trunc(to_date(DATE_ONE, 'DD-MM-YYYY HH24:MI:SS')) - trunc(DATE_TWO)) * interval '1' day > interval '15' day;
or more simply just compare with the number 15:
select field1,field2,field3
from mytable
where trunc(to_date(DATE_ONE, 'DD-MM-YYYY HH24:MI:SS')) - trunc(DATE_TWO) > 15;
db<>fiddle
I've changed trunc(to_date(DATE_TWO, 'DD-MM-YYYY')) to simply trunc(DATE_TWO). Because DATE_TWO is already a date, when you do to_date(DATE_TWO, 'DD-MM-YYYY') you are implicitly converting the date to a string using your session's NLS_DATE_FORMAT setting (which seems to be DD-MON-RR), and then converting that string back to a date. As well as being a waste of effort, that may break if this is run in a session with different settings. You just need to truncate the date you already have.

V$TRANSACTION table to find the duration of transactions

How to find the names of transactions that have been active for more than 30 minutes from the current time using the START_TIME column of V$TRANSACTION table in Oracle?
We can do arithmetic with dates in Oracle. Sysdate is the current datetime. There are 48 half-hours in a day. So:
select *
from V$TRANSACTION
where to_date(start_time, 'mm/dd/yyyy hh24:mi:ss') <= sysdate - (1/48)
Hmmm, it seems slightly odd that V$TRANSACTION.start_time uses a different format mask from sysdate. Anyway, you can avoid the ORA-01843 error with an explicit date conversion.

Query to fetch records only form previous half hour with time stamp in unix epoch format

I want SQL query to fetch/select records which are taken only from previous half an hour only. For example if my scheduler ran at 2 pm, and then again in 2:30, during the 2:30 run it should only pick rows from between 2pm and 2:30pm and not earlier, using the column created_timestamp which stores the time as unix epoch format eg:
|created_timestamp|
|1497355750350 |
|1497506182344 |
We can do arithmetic with Oracle dates. Subtracting one date from another gives the interval as a fractional number. Multiplying by 86400 gives us the number of seconds. So this is the current unix expoch:
(sysdate - date '1970-01-01') * 86400
This means your query will be something like
select * from your_table
where created_timestamp >= (:last_run_time - date '1970-01-01') * 86400
The trick is that your scheduler needs to pass in the time of the previous run - last_run_time - to pick up all the records which have been added since then.
You can do Flashback query
SELECT * FROM TABLE
AS OF TIMESTAMP (SYSTIMESTAMP - INTERVAL '30' MINUTE);

Teradata 15: [9134 : HY000] Teradata hour of day must be in range 1-12

I am trying to convert a varchar(19) timestamp field from a flat file into Teradata timestamp, but I got the following error.
select TOP 100
TO_TIMESTAMP (SOURCE_DTTM , 'YYYY-MM-DD HH:MI:SS') AS TS1
FROM "TEST"."CUSTOMER"
WHERE SOURCE_DTTM NOT LIKE '%0000-00-00%';
Executed as Single statement. Failed [9134 : HY000] Teradata hour of day must be in range 1-12
Elapsed time = 00:00:00.078
STATEMENT 1: Select Statement failed.
I am wondering if there is a way to specify the timestamp as a 24 hour format.
Really appreciate it.
I went to info.teradata.com and found the correct syntax:
select TOP 100
TO_TIMESTAMP (SOURCE_DTTM , 'YYYY-MM-DD HH24:MI:SS') AS TS1
FROM "TEST"."CUSTOMER"
WHERE SOURCE_DTTM NOT LIKE '%0000-00-00%';

Remove milli second from timstamp in oracle sql

I want to remove millisecond from 21-02-14 10:41:08.000000000 PM.
I try
select to_date('21-02-14 10:41:08.000000000 PM','DD/MM/YY HH:MI:SS:SSSSS PM') from dual;
error:
Error starting at line : 1 in command -
select to_date('21-02-14 10:41:08.000000000 PM','DD/MM/YY HH:MI:SS:PM') from dual
Error report -
SQL Error: ORA-01855: AM/A.M. or PM/P.M. required
01855. 00000 - "AM/A.M. or PM/P.M. required"
*Cause:
*Action:
You're specifying fractional seconds, but your format has "seconds past midnight" - which doesn't have 9 digits. You're also specifying a colon in your format string, when your data has a dot. Try:
select cast(to_timestamp('21-02-14 10:41:08.000000000 PM','DD-MM-YY HH:MI:SS.FF9 PM') as date) from dual;