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
Related
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 ...
I received a script from a work colleague to run when checking batches however the script works on his laptop but as soon as I run it on my device I get and error.
I don't know if it could be permissions since that's the only difference between our setups.
the script is to select certain columns convert date format with to_char for specific batch name and then filter for specific date time.
here is the sample code:
select batch_name, NO_OF_ITEMS, NUMBER_OF_ERRORS, to_char(RUN_START_DATE,'DD-MON-YYYY HH24:MI') EXECUTION_START, to_char(RUN_END_DATE,'DD-MON-YYYY HH24:MI') EXECUTION_END,
to_char(START_DATE,'DD-MON-YYYY HH24:MI') START_DATE, to_char(END_DATE,'DD-MON-YYYY HH24:MI') END_DATE, NOTES from bat_log
where BATCH_NAME in (
'SJ63R_03'
)
and RUN_START_DATE between '04-Aug-2020 07:00' AND '05-Aug-2020 07:00'
order by run_start_date desc;
error I get:
ORA-01830: date format picture ends before converting entire input string
01830. 00000 - "date format picture ends before converting entire input string"
*Cause:
*Action:
when I remove the time 07:00 from RUN_START_DATE between '04-Aug-2020 07:00' AND '05-Aug-2020 07:00' the script runs perfectly but obviously doesn't filter for the time frame.
any suggestions would be appreciated.
I tried to replicate a bit your scenario
SQL> create table t ( c1 date , c2 date ) ;
Table created.
SQL> insert into t values ( to_date('01/08/2020 11:00:00','DD/MM/YYYY HH24:MI:SS') , to_date('01/08/2020 17:00:00','DD/MM/YYYY HH24:MI:SS') ) ;
1 row created.
SQL> insert into t values ( to_date('01/09/2020 11:00:00','DD/MM/YYYY HH24:MI:SS') , to_date('01/09/2020 17:00:00','DD/MM/YYYY HH24:MI:SS') ) ;
1 row created.
SQL> select * from t ;
C1 C2
--------- ---------
01-AUG-20 01-AUG-20
01-SEP-20 01-SEP-20
SQL> select c1 , c2 from t where to_char(c1,'DD-MON-YYYY HH24:MI') between '02-AUG-2020 08:00' and '02-SEP-2020 08:00' ;
no rows selected
SQL> select c1 , c2 , to_char(c1,'DD-MON-YYYY HH24:MI') as result from t ;
C1 C2 RESULT
--------- --------- --------------------------
01-AUG-20 01-AUG-20 01-AUG-2020 11:00
01-SEP-20 01-SEP-20 01-SEP-2020 11:00
SQL> select c1 , c2 from t where to_char(c1,'DD-MON-YYYY HH24:MI') between to_date('02-AUG-2020 08:00','DD-MON-YYYY HH24:MI')
2 and to_date('02-SEP-2020 08:00','DD-MON-YYYY HH24:MI') ;
select c1 , c2 from t where to_char(c1,'DD-MON-YYYY HH24:MI') between to_date('02-AUG-2020 08:00','DD-MON-YYYY HH24:MI')
*
ERROR at line 1:
ORA-01830: date format picture ends before converting entire input string
SQL> alter session set nls_date_format='DD-MON-YYYY HH24:MI' ;
Session altered.
SQL> select c1 , c2 from t where c1 between to_date('02-AUG-2020 08:00','DD-MON-YYYY HH24:MI')
and to_date('02-SEP-2020 08:00','DD-MON-YYYY HH24:MI') ; 2
C1 C2
----------------- -----------------
01-SEP-2020 11:00 01-SEP-2020 17:00
SQL>
If the original fields are dates, you might change your NLS date settings to make it work, as well as applying to_date to the strings you want to compare as dates
Update
I remove the to_char to avoid an implicit conversion.
Yes, something is different between your two setups. It is the value of NLS_DATE_FORMAT.
Your query is depending on an implicit TO_DATE() to convert the strings '04-Aug-2020 07:00' AND '05-Aug-2020 07:00' from strings to DATE, which is an internal, binary data type. Since you are relying on implicit conversion, you are also relying on the underlying value of NLS_DATE_FORMAT to match the strings you are supplying. This is why I always, always, always use to_date() and include the proper format mask:
RUN_START_DATE between to_date('04-Aug-2020 07:00','dd-Mon-yyyy hh24:mi:ss') AND to_date('05-Aug-2020 07:00','dd-Mon-yyyy hh24:mi:ss')
For more background, see https://edstevensdba.wordpress.com/2011/04/07/nls_date_format/
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'
I am using oracle 11g and I have normal timestamps (starttime) which produce an output as follows:
23.09.14 05:15:00,000000000
Now I want an output like
23.09.14 05
Also ok would be:
23.09.14 05:00:00,000000000
but when I use something like
round(starttime, 'HH') or trunc(starttime ,'HH24') I always get
23.09.14
with no hours at all.
Looking around here at stackoverflow I found
substr(TO_CHAR(starttime),0,LENGTH(TO_CHAR(starttime))-13)
which produces the correct output as char but when I want to sort dates it wont work because it sorts alphabetically. (so for example, 1.3., 1.4, 1.5.... instead of 1.3., 2.3., 3.3,...),
Any idea how I can get a timestamp which is rounded to the full hour?
I will have to use the statement in a group by clause. The complete statement would look like:
select round(starttime, 'HH24'), sum(counter) from wmsconsolidationorderdwct group by round(starttime, 'HH24') order by round(starttime, 'HH24') desc;
So I cannot display the rounded time and sort by the full timestamp since this would violate the group by clause.
This will truncate to the hour:
trunc(SYSTIMESTAMP) + extract(hour from SYSTIMESTAMP)/24
Edit:
I just tried it and
SELECT TRUNC(SYSTIMESTAMP ,'HH24') FROM DUAL;
returns the correct result.
Fiddle
If your purpose is to display, then use TO_CHAR with desired format model.
For example,
SQL> SELECT TO_CHAR(SYSTIMESTAMP, 'DD.MM.YY HH24') FROM dual;
TO_CHAR(SYS
-----------
28.05.15 15
SQL>
If your purpose is to do date arithmetic then you need to leave the data type as date.
For example,
SQL> alter session set nls_date_format='DD-MM-YYYY HH24:MI:SS'
2 /
Session altered.
SQL> SELECT TRUNC(SYSTIMESTAMP ,'HH24') FROM DUAL
2 /
TRUNC(SYSTIMESTAMP,
-------------------
28-05-2015 15:00:00
SQL>
If you have a timestamp object:
select my_number from(
SELECT TO_NUMBER(TO_CHAR(TO_DATE (TO_CHAR (SYSTIMESTAMP, 'DD.MM.YY HH24:MI'),
'DD.MM.YY HH24:MI'
),'HH24')) AS my_number
FROM DUAL)
order by 1
This could be simplified to:
select my_number from(
SELECT TO_NUMBER(TO_CHAR(SYSTIMESTAMP, 'HH24')) AS my_number
FROM DUAL)
order by 1
I have two timestamps in String format 2015-05-06T15:39:00 and 2015-04-06T15:39:00.
What is the sql query for Oracle that I can query all the records in the table that has timestamp that falls within this range.
And with alternative way you can use between
SELECT *
FROM tab1
WHERE timestamps BETWEEN TO_DATE ('2015-05-06T15:39:00', 'YYYY-MM-DD"T"HH24:MI:SS') AND TO_DATE('2015-04-06T15:39:00', 'YYYY-MM-DD"T"HH24:MI:SS');
SELECT *
FROM yourTable
WHERE timestamps >= TO_DATE('2015-05-06T15:39:00', 'YYYY-MM-DD"T"HH24:MI:SS')
AND timestamps <= TO_DATE('2015-04-06T15:39:00', 'YYYY-MM-DD"T"HH24:MI:SS')
None of the above answers worked for me.
however, First simply replace 'T' you have with a ' '(space) and use below query which worked for me
select * from tb1
where timestamps BETWEEN TO_DATE ('2015-05-06 15:39:00', 'YYYY-mm-dd HH24:MI:SS')
AND TO_DATE('2015-04-06 15:39:00', 'YYYY-mm-dd HH24:MI:SS');
You need to convert the literal into DATE using TO_DATE and required format mask to compare the timestamp column with the input timestamp values.
Setup
SQL> CREATE TABLE t(A TIMESTAMP);
Table created.
SQL>
SQL> INSERT INTO t(A) VALUES(to_date('2015-04-10T15:39:00', 'YYYY-MM-DD"T"HH24:MI:SS'));
1 row created.
SQL> INSERT INTO t(A) VALUES(to_date('2015-05-01T15:39:00', 'YYYY-MM-DD"T"HH24:MI:SS'));
1 row created.
SQL> INSERT INTO t(A) VALUES(to_date('2015-03-01T15:39:00', 'YYYY-MM-DD"T"HH24:MI:SS'));
1 row created.
SQL> COMMIT;
Commit complete.
SQL> SELECT * FROM t;
A
----------------------------
10-APR-15 03.39.00.000000 PM
01-MAY-15 03.39.00.000000 PM
01-MAR-15 03.39.00.000000 PM
Query
SQL> SELECT *
2 FROM t
3 WHERE A BETWEEN
4 to_date('2015-04-06T15:39:00', 'YYYY-MM-DD"T"HH24:MI:SS')
5 AND
6 to_date('2015-05-06T15:39:00', 'YYYY-MM-DD"T"HH24:MI:SS');
A
--------------------------------------------------------------------------
10-APR-15 03.39.00.000000 PM
01-MAY-15 03.39.00.000000 PM
So, I got the required rows as my desired output.
The below one is for timestamp and you can change for your required time
SELECT *
FROM tbl1
WHERE timestamp BETWEEN to_date('21/11/2017 23:59:59','dd/MM/rrrr hh24:mi:ss')
AND to_date('21/12/2017 15:59:59','dd/MM/rrrr hh24:mi:ss');`