current date in sql oracle [duplicate] - sql

This question already has answers here:
how to add second in oracle timestamp
(2 answers)
How to add 10 seconds in current_timestamp SQL ( Oracle )
(2 answers)
Closed 3 years ago.
I have a problem with the query in jdbc logstash.: z.clock < to_unix_timestamp(sysdate)-10 seconds
how to save system date minus 10 seconds?
z.clock is Unix timestamp.
SELECT h.name as hostname,i.name as item,i.key_,z.clock,z.value FROM zabbix.hosts h where z.clock > :sql_last_value and z.clock < to_unix_timestamp(sysdate)-10 seconds

In Oracle, one option is to subract it as
SQL> alter session set nls_date_format = 'dd.mm.yyyy hh24:mi:ss';
Session altered.
SQL> select sysdate as right_now,
2 sysdate - 10 / (24 * 60 * 60) as ten_seconds_ago,
3 sysdate - interval '10' second ten_seconds_ago_2
4 from dual;
RIGHT_NOW TEN_SECONDS_AGO TEN_SECONDS_AGO_2
------------------- ------------------- -------------------
31.07.2019 11:16:07 31.07.2019 11:15:57 31.07.2019 11:15:57
SQL>

This could be a way:
select sysdate - interval '10' second
from dual
For example:
SQL> select to_char(sysdate - interval '10' second, 'dd/mm/yyyy hh24:mi:ss') as d_minus_10,
2 to_char(sysdate, 'dd/mm/yyyy hh24:mi:ss') as d
3 from dual;
D_MINUS_10 D
------------------- -------------------
31/07/2019 11:18:46 31/07/2019 11:18:56

Related

Add minutes to an Oracle date

I am trying to add minutes to my sql but no error and no data.
This is my sql
select distinct mo.reference_no payment_id, mo.dcn message_id,
mo.amount, mo.ccy, decode (mo.msg_status, 'R', 'Repair', 'P',
'Processed','N','Ungenerated','G','Generated',mo.msg_status)
message_status, to_char
(MO.INSERT_TIME,'DD-MM-YYYY HH24:MI:SS')
paym_date, mo.branch_date, mo.maker_id
from table1 mo
left join table2 mi
on mo.reference_no = mi.generated_ref_no
where mo.swift_msg_type = 103
and mo.ccy = 'XXX' and mo.branch_date = trunc(sysdate)
and mo.msg_status in('R','N','G','P')
order by PAYM_DATE desc;
And I want to add 5 minutes to (MO.INSERT_TIME,'DD-MM-YYYY HH24:MI:SS')
So I want my sql show me after 5 minutes for this column (MO.INSERT_TIME,'DD-MM-YYYY HH24:MI:SS')
I wrote sql like that
select distinct mo.reference_no payment_id, mo.dcn message_id,
mo.amount, mo.ccy, decode (mo.msg_status, 'R', 'Repair', 'P',
'Processed','N','Ungenerated','G','Generated',mo.msg_status)
message_status, to_char
(MO.INSERT_TIME,'DD-MM-YYYY HH24:MI:SS')
paym_date, mo.branch_date, mo.maker_id
from table1 mo
left join table2 mi
on mo.reference_no = mi.generated_ref_no
where mo.swift_msg_type = 103
and mo.ccy = 'XXX' and mo.branch_date = trunc(sysdate)
and MO.INSERT_TIME = to_date (sysdate,'DD-MM-YYYY HH24:MI:SS') + INTERVAL '5' MINUTE
and mo.msg_status in('R','N','G','P')
order by PAYM_DATE desc;
And after 5 minutes I am selecting this sql and their is no data. Can you explain why?
This is what you used:
and MO.INSERT_TIME = to_date (sysdate,'DD-MM-YYYY HH24:MI:SS') + INTERVAL '5' MINUTE
It is wrong because you're applying TO_DATE function to SYSDATE which already returns DATE datatype, so there's no point in doing it.
SQL> alter session set nls_date_format = 'dd.mm.yyyy hh24:mi:ss';
Session altered.
SQL> select sysdate,
2 sysdate + interval '5' minute
3 from dual;
SYSDATE SYSDATE+INTERVAL'5'
------------------- -------------------
10.01.2023 09:49:05 10.01.2023 09:54:05
SQL>
It is probably not very likely that MO.INSERT_TIME will exactly be the same as SYSDATE (which is right now) + 5 minutes, which is in the future.
Perhaps you'd rather set that condition to e.g. rows inserted during last 5 minutes (which makes more sense to me):
and MO.INSERT_TIME >= sysdate - INTERVAL '5' MINUTE
DATE + NUMBER where number can be fractional and is interpreted as days, and a minute is 1/(24*60) of a day...

Data in Oracle apex

If I want to take data for today from 00:00 until currently hour how can I do it ???
I have this table
datetime
hourly
clientchannel
servicename
service_count
13_02_2022
9
*****
notification
2
Presuming that datetime column's datatype is DATE (should be), then
select *
from your_table
where datetime between trunc(sysdate) and trunc(sysdate, 'hh24')
because
SQL> alter session set nls_date_format = 'dd.mm.yyyy hh24:mi:ss';
Session altered.
SQL> select sysdate as right_now,
2 trunc(sysdate) as midnight,
3 trunc(sysdate, 'hh24') this_hour
4 from dual;
RIGHT_NOW MIDNIGHT THIS_HOUR
------------------- ------------------- -------------------
01.03.2022 08:01:20 01.03.2022 00:00:00 01.03.2022 08:00:00
SQL>
If datetime's datatype is VARCHAR2 (bad choice), then you should first convert it to date, applying correct format model and hoping that there's no garbage in that column:
where to_date(datetime, 'dd_mm_yyyy') between ...

Oracle Query to fetch data based on only time condition

I need to fetch the data for last six month based on when modified date but the records I need are having time greater than 1500H. Please assist.
I have tried below condition in my script but it's not working:
AND WHENMODIFIEDDATE >= to_date('2021-01-01 00:00:00', 'YYYY-MM-DD HH24:MI:SS')
AND WHENMODIFIEDDATE <= to_date('2021-06-01 23:59:59','YYYY-MM-DD HH24:MI:SS')
AND WHENMODIFIEDDATE >= to_date('15:00:00', 'HH24:MI:SS') ;
Data Type for column WHENMODIFIEDDATE: DATE
Sample Data:
AUDITORKEY AUDITORID AUDITORNAME STATUS WHENMODIFIEDDATE
9266165 xyz xyz A 2020-10-21 08:13:43.0
The phrase
AND WHENMODIFIEDDATE >= to_date('15:00:00', 'HH24:MI:SS')
does not look at hours. If you use the TO_DATE command on just a time component, we will automatically construct a FULL date, eg
SQL> alter session set nls_date_format = 'dd/mm/yyyy hh24:mi:ss';
Session altered.
SQL> select to_date('15:00:00', 'HH24:MI:SS') from dual;
TO_DATE('15:00:00',
-------------------
01/08/2021 15:00:00
This is why your third predicate is having no effect (or could easily have the wrong effect depending on your date criteria).
As others mentioned in the comments, if you want to get just the HOUR from a date, you can use options such as EXTRACT or TO_CHAR depending on the data type of the source column
SQL> create table t ( x timestamp , y date) ;
Table created.
SQL> insert into t values (localtimestamp, sysdate);
1 row created.
SQL> select extract(HOUR from x) from t;
EXTRACT(HOURFROMX)
------------------
11
SQL> select extract(HOUR from y) from t;
select extract(HOUR from y) from t
*
ERROR at line 1:
ORA-30076: invalid extract field for extract source
SQL> select to_number(to_char(y,'HH24')) from t;
TO_NUMBER(TO_CHAR(Y,'HH24'))
----------------------------
11

Oracle sql date interval

Please help me B is correct?
or C
Your best option is to try it.
SQL> alter session set nls_date_format = 'dd_mm_yyyy';
Session altered.
SQL> -- A
SQL> select to_char(to_date('29-10-2019') + interval '2' month + interval '6' day
2 + interval '120' second, 'dd-mon-yyyy') as datum from dual;
DATUM
-----------
04-jan-2020
SQL> -- B
SQL> select to_char(to_date('29-10-2019') + interval '3' month + interval '7' day
2 + interval '120' second, 'dd-mon-yyyy') as datum from dual;
DATUM
-----------
05-feb-2020
SQL> -- C
SQL> select to_char(to_date('29-10-2019') + interval '2' month + interval '4' day
2 + interval '120' second, 'dd-mon-yyyy') as datum from dual;
DATUM
-----------
02-jan-2020
SQL> -- D
SQL> select to_char(to_date('29-10-2019') + interval '2' month + interval '6' day
2 + interval '120' second, 'dd-mon-yyyy') as datum from dual;
DATUM
-----------
04-jan-2020
SQL> -- E
SQL> select to_char(to_date('29-10-2019') + interval '2' month + interval '5' day
2 + interval '120' second, 'dd-mon-yyyy') as datum from dual;
DATUM
-----------
03-jan-2020
SQL>
So - yes, C is correct.
None, all the queries are syntactically invalid and will raise exceptions as:
they have backticks instead of single quotes;
there should be a second quote in INTEVAL '120 SECOND and INTERVAL is misspelt; and
around the format model for the TO_CHAR function the quotes are missing and so is the preceding comma to separate the arguments.
If you ignore the (many) syntax errors then you start with 2019-10-29 and want to get to 2020-01-02; there is a difference of 2 months (which would take you to 2019-12-29) and 4 days. The seconds do not matter as they are not being displayed due to the NLS settings. This would give the answer C.

Converting time difference to a given format in Oracle

How do I convert EVENT_DATE_B - EVENT_DATE_A which is a number of days to string with HH:MM format?
Another approach (one query can be on different days):
with tt as (
select numToDsinterval((EVENT_DATE_B - EVENT_DATE_A ), 'DAY') dsint
from t)
select (extract(day from dsint)*24)+extract(hour from dsint) ||
':' ||extract(minute from dsint)
from tt
Here is a sqlfiddle demo
If dates are differ only in time part you can use interval day to second. For instance:
SQL> select (to_date('25.12.12 15:37:32', 'DD.MM.YY HH24:MI:SS')
2 - to_date('25.12.12 12:45:45', 'DD.MM.YY HH24:MI:SS')) day(0) to second(0) as Time
3 from dual
4 ;
TIME
-------------
+0 02:51:47
But obviously it will not always be the case. So you could write a long query to calculate different parts of time, but I think I would go with this simple function:
SQL> create or replace function DaysToTime(p_val in number)
2 return varchar2
3 is
4 l_hours number;
5 l_minutes number;
6 l_seconds number;
7 begin
8 l_Hours := 24 * p_val;
9 l_minutes := (l_hours - trunc(l_hours)) * 60;
10 l_seconds := (l_minutes - trunc(l_minutes)) * 60;
11 return to_char(trunc(l_hours), 'fm09') ||':'||
12 to_char(trunc(l_minutes), 'fm09')||':'||
13 to_char(trunc(l_seconds), 'fm09');
14 end;
15 /
Function created
And now the query would be:
SQL> select DaysToTime(to_date('25.12.12 15:37:32', 'DD.MM.YY HH24:MI:SS')
2 - to_date('25.12.12 12:45:45', 'DD.MM.YY HH24:MI:SS')) as Time
3 from dual
4 ;
TIME
----------
02:51:47
select 24 * (EVENT_DATE_B - EVENT_DATE_A) || ':' || '00'
from your_table
SQLFiddle demo
I think you need to find out the number of days between date1 and date2, then subtract this difference from date2 and convert final date to your format. Copy/paste and see the output:
Select date2, days_between, to_char(date2-days_between, 'mm-dd-yyyy hh24:mi:ss') end_date
From
(
Select sysdate date2
, trunc(sysdate)-to_date('20-DEC-2012') days_between --'20-DEC' is start_date
From dual
)
/