filter timestamp column in SQL Oracle - sql

I have a table with a Timestamp column which I need to filter after '2020-08-26', but every solution won't work
RUN_TIME
2020-07-22 04:22:07
2020-07-22 04:34:07
2020-07-22 04:45:07
2020-07-22 04:50:07
2020-07-22 04:55:08
I tried the below queries:
WHERE CAST (RUN_DATE AS DATE) >= TO_DATE('2020-08-26', 'yyyy-mm-dd')
WHERE to_date(RUN_DATE, 'YYYY-MM-DD hh24:mi:ss') >= to_date('26-08-2020 23:59:59', 'YYYY-MM-DD hh24:mi:ss')
WHERE RUN_DATE >= TO_TIMESTAMP('26-08-2020 23:59:59')

Use a TIMESTAMP literal:
SELECT *
FROM table_name
WHERE run_time >= TIMESTAMP '2020-08-27 00:00:00';
or, use a DATE literal:
SELECT *
FROM table_name
WHERE run_time >= DATE '2020-08-27';
or, use TO_TIMESTAMP with a format model:
SELECT *
FROM table_name
WHERE run_time >= TO_TIMESTAMP( '2020-08-27', 'YYYY-MM-DD' );
or, use TO_DATE with a format model:
SELECT *
FROM table_name
WHERE run_time >= TO_DATE( '2020-08-27', 'YYYY-MM-DD' );
Which for the sample data:
CREATE TABLE TABLE_NAME ( run_time TIMESTAMP(6) );
INSERT INTO table_name ( run_time )
SELECT TIMESTAMP '2020-07-22 04:22:07' FROM DUAL UNION ALL
SELECT TIMESTAMP '2020-07-22 04:34:07' FROM DUAL UNION ALL
SELECT TIMESTAMP '2020-07-22 04:45:07' FROM DUAL UNION ALL
SELECT TIMESTAMP '2020-07-22 04:50:07' FROM DUAL UNION ALL
SELECT TIMESTAMP '2020-07-22 04:55:08' FROM DUAL UNION ALL
SELECT TIMESTAMP '2020-08-26 23:59:59.999999' FROM DUAL UNION ALL
SELECT TIMESTAMP '2020-08-27 00:00:00' FROM DUAL;
All output:
| RUN_TIME |
| :------------------------ |
| 27-AUG-20 00.00.00.000000 |
db<>fiddle here

Just:
where run_date >= date '2020-08-26'
That's direct filtering against a date literal.
run_date is a timestamp already, so you should not attempt to convert it. Also, to_timestamp() takes a second argument, which specifies the format of the string; without it, it defaults to the nls_timestamp_format of your session (or database), which may not be what you expect

Related

How can i insert DateTime in SQL?

how can i insert this DateTime in SQL?
1/21/2020 8:30:53.527000 AM +00:00
I know that for 12/12/2020 we can make TO_DATE('12/12/2020', 'DD/MM/YYYY')
In Oracle, in your INSERT statement you can use a TIMESTAMP literal:
TIMESTAMP '2020-01-21 08:30:53.527000 +00:00'
Or use TO_TIMESTAMP_TZ:
TO_TIMESTAMP_TZ(
'1/21/2020 8:30:53.527000 AM +00:00',
'MM/DD/YYYY HH12:MI:SS.FF6 AM TZH:TZM'
)

How to "flatten" the structure with the TABLE expression

I have a VARRAYS TYPE wanted to "flatten" the structure with the TABLE expression.
The example data
CREATE OR REPLACE TYPE ARIS.NUMBER_VARRAY_5 AS VARRAY (5) OF NUMBER NOT NULL;
CREATE TABLE ARIS.TEST_2
(
DATE_START DATE,
DATE_END DATE,
O3 ARIS.NUMBER_VARRAY_5,
CATEGORY NUMBER
)
SET DEFINE OFF;
Insert into TEST_2
(DATE_START, DATE_END, O3, CATEGORY)
Values
(TO_DATE('01/01/2005 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), TO_DATE('01/05/2005 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), NUMBER_VARRAY_5(281.25,-9999,291.5,310.5,298.75), NULL);
Insert into TEST_2
(DATE_START, DATE_END, O3, CATEGORY)
Values
(TO_DATE('01/02/2005 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), TO_DATE('01/06/2005 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), NUMBER_VARRAY_5(-9999,291.5,310.5,298.75,300.75), NULL);
COMMIT;
using the SQL statement
select O3.* from test_2 t, table(t.o3) O3
I can get the result
COLUMN_VALUE
281.25
-9999
291.5
310.5
298.75
-9999
291.5
310.5
298.75
300.75
The question is how I get the results like
DATE_START DATE_END 03.VALUE1 03.VALUE2 03.VALUE3 03.VALUE4 03.VALUE5
01/01/2005 01/05/2005 281.25 -9999 291.5 310.5 298.75
01/02/2005 01/06/2005 -9999 291.5 310.5 298.75 300.75
Try This,
SELECT
*
FROM
(
select
t.Date_start,
t.Date_end,
row_number() OVER ( PARTITION BY DATE_START, DATE_END
ORDER BY
ROWNUM) rn,
O3.*
from
test_2 t,
table(t.o3) O3
)
PIVOT ( MAX(column_value) FOR rn in
(
1 as "03.VALUE1",
2 as "03.VALUE2",
3 as "03.VALUE3",
4 as "03.VALUE4",
5 as "03.VALUE5"
)
) ;

how to add AM or PM in PostgreSQL in date format itself?

select to_date(to_char(sysdate,'yyyy-mm-dd hh12:mi:ss AM'),
'yyyy-MM-dd HH12:MI:SS AM') from dual;
This works fine in oracle but not in Postgres.
Instead of sysdate you should use current_timestamp, or now():
SELECT TO_CHAR(current_timestamp, 'yyyy-mm-dd hh12:mi:ss AM')
A table for add AM or PM format itself.
create table date_tbl
(
id serial,
Date_time varchar (30) default TO_CHAR(current_timestamp, 'yyyy-mm-dd hh:mi:ss AM')
)
Try the below function to accept time stamp with AM/PM over PostgreSQL
TO_CHAR(SYSDATE, 'MM/DD/YYYY HH12:MI:SS AM')

Oracle: How to filter by date and time in a where clause

How can I do this:
select *
from tableName
where SESSION_START_DATE_TIME > To_Date ('12-Jan-2012 16:00', 'DD-MON-YYYY hh24:mi' )
SESSION_START_DATE_TIME is in the format '12/01/2012 13:16:32.000'
I tried where To_Date (SESSION_START_DATE_TIME, 'DD-MON-YYYY hh24:mi') > To_Date ('12-Jan-2012 16:00', 'DD-MON-YYYY hh24:mi' )
but no matter what I try I get the error:
SQL command not properly formed
In the example that you have provided there is nothing that would throw a SQL command not properly formed error. How are you executing this query? What are you not showing us?
This example script works fine:
create table tableName
(session_start_date_time DATE);
insert into tableName (session_start_date_time)
values (sysdate+1);
select * from tableName
where session_start_date_time > to_date('12-Jan-2012 16:00', 'DD-MON-YYYY hh24:mi');
As does this example:
create table tableName2
(session_start_date_time TIMESTAMP);
insert into tableName2 (session_start_date_time)
values (to_timestamp('01/12/2012 16:01:02.345678','mm/dd/yyyy hh24:mi:ss.ff'));
select * from tableName2
where session_start_date_time > to_date('12-Jan-2012 16:00', 'DD-MON-YYYY hh24:mi');
select * from tableName2
where session_start_date_time > to_timestamp('01/12/2012 14:01:02.345678','mm/dd/yyyy hh24:mi:ss.ff');
So there must be something else that is wrong.
If SESSION_START_DATE_TIME is of type TIMESTAMP you may want to try using the SQL function TO_TIMESTAMP. Here is an example:
SQL> CREATE TABLE t (ts TIMESTAMP);
Table created.
SQL> INSERT INTO t
2 VALUES (
3 TO_TIMESTAMP (
4 '1/12/2012 5:03:27.221008 PM'
5 ,'mm/dd/yyyy HH:MI:SS.FF AM'
6 )
7 );
1 row created.
SQL> SELECT *
2 FROM t
3 WHERE ts =
4 TO_TIMESTAMP (
5 '1/12/2012 5:03:27.221008 PM'
6 ,'mm/dd/yyyy HH:MI:SS.FF AM'
7 );
TS
-------------------------------------------------
12-JAN-12 05.03.27.221008 PM
Put it this way
where ("R"."TIME_STAMP">=TO_DATE ('03-02-2013 00:00:00', 'DD-MM-YYYY HH24:MI:SS')
AND "R"."TIME_STAMP"<=TO_DATE ('09-02-2013 23:59:59', 'DD-MM-YYYY HH24:MI:SS'))
Where
R is table name.
TIME_STAMP is FieldName in Table R.
Try:
To_Date (SESSION_START_DATE_TIME, 'MM/DD/YYYY hh24:mi') >
To_Date ('12-Jan-2012 16:00', 'DD-MON-YYYY hh24:mi' )
Obviously '12/01/2012 13:16:32.000' doesn't match 'DD-MON-YYYY hh24:mi' format.
Update:
You need 'MM/DD/YYYY hh24:mi:ss.ff' format and to use TO_TIMESTAMP instead of TO_DATE cause dates don't hold millis in oracle.

SQL/Oracle Grouping Data by field by hours for a certain type on a given day

Okay trying to construct a single query to save myself a whole bunch of time (rather than writing a ton of seperate queries), but I don't even know how to start on this.
What I need to is look at single day and type and break out counts on actions, by hour, between 8:00am - 8:00pm. So for example I have the following fake table
TYPE_ ACTION_ TIMESTAMP_
------------------------------
A processed 2010-11-19 10:00:00.000
A processed 2010-11-19 10:46:45.000
A processed 2010-11-19 11:46:45.000
A processed 2010-11-19 12:46:45.000
A processed 2010-11-19 12:48:45.000
A pending 2010-11-19 11:46:45.000
A pending 2010-11-19 11:50:45.000
A pending 2010-11-19 12:46:45.000
A pending 2010-11-19 12:48:45.000
B pending 2010-11-19 19:48:45.000
B pending 2010-11-19 21:46:45.000
.etc
So if I wanted to look at all records with
TYPE_ = 'A'
on date 2010-11-19
grouped by ACTION_ per hour
I would see this result
ACTION_ NUMOCCURENCES RANGE
---------------------------------------------
processed 2 10:00:00 - 11:00:00
pending 0 10:00:00 - 11:00:00
processed 1 11:00:00 - 12:00:00
pending 2 11:00:00 - 12:00:00
processed 2 12:00:00 - 13:00:00
pending 2 12:00:00 - 13:00:00
Or something similar to that, but that should at least give an idea of what I am looking for.
Can anyone help? Normally I would try to provide some sample code I'm working with, but I have no idea how I would work with the group by clauses needed to make this happen.
select
action_,
count(*) as numoccurences,
to_char(timestamp_ , 'hh24') || ':00:00-' ||
to_char(timestamp_ + 1/24, 'hh24') || ':00:00' as range
from
tq84_action
where
timestamp_ between timestamp '2010-11-19 08:00:00' and
timestamp '2010-11-19 20:00:00' and
type_ = 'A'
group by
action_,
to_char(timestamp_ , 'hh24') || ':00:00-' ||
to_char(timestamp_ + 1/24, 'hh24') || ':00:00'
order by
range;
Now, the above select statement only returns hours in which there is at least on action. In order to show a record for all hour - {processed/pending} combinations, the following amendments should be made to the query:
select
action_,
count(type_) as numoccurences,
to_char(timestamp_ , 'hh24') || ':00:00-' ||
to_char(timestamp_ + 1/24, 'hh24') || ':00:00' as range_
from (
select * from tq84_action
where
timestamp_ between timestamp '2010-11-19 08:00:00' and
timestamp '2010-11-19 20:00:00' and
type_ = 'A'
union all (
select
null as type_,
action.name_ as action_,
date '2010-11-19' + 8/24 + hour.counter_ / 24 as timestamp_1
from (
select
level-1 counter_
from dual
connect by level <= 12
) hour,
(
select 'processed' as name_ from dual union all
select 'pending' as name_ from dual
) action
)
)
group by
action_,
to_char(timestamp_ , 'hh24') || ':00:00-' ||
to_char(timestamp_ + 1/24, 'hh24') || ':00:00'
order by
range_;
BTW, here's the DDL and DML I used:
drop table tq84_action;
create table tq84_action (
type_ varchar2( 1),
action_ varchar2(10),
timestamp_ timestamp
);
insert into tq84_action values('A', 'processed' , timestamp '2010-11-19 10:00:00.000');
insert into tq84_action values('A', 'processed' , timestamp '2010-11-19 10:46:45.000');
insert into tq84_action values('A', 'processed' , timestamp '2010-11-19 11:46:45.000');
insert into tq84_action values('A', 'processed' , timestamp '2010-11-19 12:46:45.000');
insert into tq84_action values('A', 'processed' , timestamp '2010-11-19 12:48:45.000');
insert into tq84_action values('A', 'pending' , timestamp '2010-11-19 11:46:45.000');
insert into tq84_action values('A', 'pending' , timestamp '2010-11-19 11:50:45.000');
insert into tq84_action values('A', 'pending' , timestamp '2010-11-19 12:46:45.000');
insert into tq84_action values('A', 'pending' , timestamp '2010-11-19 12:48:45.000');
insert into tq84_action values('B', 'pending' , timestamp '2010-11-19 19:48:45.000');
insert into tq84_action values('B', 'pending' , timestamp '2010-11-19 21:46:45.000');
select
ACTION_
count(*) NUMOCCURENCES,
to_char(TIMESTAMP_, 'hh24') || ':00:00 - ' || to_char(TIMESTAMP_ + 1/24, 'hh24') || ':00:00' RANGE
from tbl
where TIMESTAMP_ between DATE '2010-11-19' and DATE '2010-11-20'
and TYPE_ = 'A'
and 1 * to_char(TIMESTAMP_, 'hh24') between 8 and 19
group by ACTION_, to_char(TIMESTAMP_, 'hh24'), to_char(TIMESTAMP_ + 1/24, 'hh24')
order by RANGE, ACTION_ desc