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 ...
Related
Is it possible to turn the current time stamp to a whole number?
Example: If sysdate returns 1/19/2022 5:36:49 PM can I turn that to 1/19/2022 5PM since it falls in the 5PM range.
Here is my query
Select FACILITY, TRK_ID, LOT_DTTM, IN_QTY
from TRK_ID_LOT
WHERE facility in 'DP1DM5'
and trk_id like ('AE%')
and lot_dttm > sysdate - 1
EXAMPLE:
Truncate it to hours:
SQL> select trunc(to_date('1/19/2022 5:36:49 PM', 'mm/dd/yyyy hh:mi:ss pm'), 'hh') res
2 from dual;
RES
----------------------
01/19/2022 05:00:00 PM
SQL>
If you want to update rows, do so using the same function:
update your_table set
date_column = trunc(date_column, 'hh');
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
I have a date a field that displays date in the format '19-JAN-20' I would like to display it as 'January 2019' using oracle database.
select '19-JAN-19' FROM DUAL
You need to use TO_CHAR to display in the required format:
select to_char(DATE '2019-01-20', 'Month YYYY') dt from dual;
DT
-------------
January 2019
select '19-JAN-19' FROM DUAL
There are couple of issues with that statement:
'19-JAN-19' is a string, not a date. Always use TO_DATE to explicitly convert it to date, or stick to ANSI date literal like I used in my demo.
Do not use two-digit YY representation for year, that's the reason Y2K bug was introduced. Always use YYYY.
Looks like a nested TO_CHAR, if "date" value you mentioned - 19-JAN-20 is stored as a string:
SQL> with test (col) as
2 (select '19-JAN-20' from dual)
3 select
4 to_char(
5 to_date(col, 'dd-mon-yy', 'nls_date_language = english'),
6 'fmMonth yyyy', 'nls_date_language = english'
7 ) result
8 from test;
RESULT
--------------
January 2020
SQL>
If your database speaks English, you can omit the NLS_DATE_LANGUAGE part (mine speaks Croatian so I included it).
However, if it is a date you'd just want to display differently, then:
(Just to avoid NLS_DATE_LANGUAGE in TO_DATE):
SQL> alter session set nls_date_language = 'english';
Session altered.
Default format in my database:
SQL> select sysdate from dual;
SYSDATE
--------
08.06.20
Alter session to desired format:
SQL> alter session set nls_date_format = 'fmMonth yyyy';
Session altered.
SQL> select sysdate from dual;
SYSDATE
--------------
June 2020
Or, apply TO_DATE function with desired format mask:
SQL> select to_char(sysdate, 'fmMonth yyyy') result from dual;
RESULT
--------------
June 2020
SQL>
Let say i have this table and i have inserted alot of date into it.
CREATE TABLE dateOfTransaction(
TransDATE DATE NOT NULL);
I need to convert the Transdate to number datatype first? then do
SELECT AVG(TransDate) FROM dateOfTransaction;
I don't quite understand what that "average date" is, but - just for fun - have a look at his:
Set date format (to see what is what):
SQL> alter session set nls_date_format = 'dd.mm.yyyy hh24:mi:ss';
Session altered.
Sample data:
SQL> create table dateoftransaction
2 (transdate date);
Table created.
SQL> insert into dateoftransaction
2 select sysdate from dual union all
3 select date '2018-11-23' from dual union all
4 select to_date('30.09.2025 13:32:33', 'dd.mm.yyyy hh24:mi:ss') from dual;
3 rows created.
SQL> select * from dateoftransaction;
TRANSDATE
-------------------
13.11.2019 20:59:00
23.11.2018 00:00:00
30.09.2025 13:32:33
SQL>
Average date is - of course - invalid:
SQL> select avg(transdate) from dateoftransaction;
select avg(transdate) from dateoftransaction
*
ERROR at line 1:
ORA-00932: inconsistent datatypes: expected NUMBER got DATE
But, what if we convert it to a Julian date?
SQL> select transdate, to_char(transdate, 'J') julian_date from dateoftransaction;
TRANSDATE JULIAN_
------------------- -------
13.11.2019 20:59:00 2458801
23.11.2018 00:00:00 2458446
30.09.2025 13:32:33 2460949
That is a number, and yes - we know how to calculate average value on numbers, right?
SQL> select
2 avg(to_number(to_char(transdate, 'J'))) avg_julian_date,
3 to_date(trunc(avg(to_number(to_char(transdate, 'J')))), 'JSP') avg_date
4 from dateoftransaction;
AVG_JULIAN_DATE AVG_DATE
--------------- -------------------
2459398,67 02.07.2021 00:00:00
See if it helps.
Subtract a base date, take the average, and add back to the base:
select date '2000-01-01' + avg(dateOfTransaction - date '2000-01-01') * interval '1 day'
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