How to fetch records according to SYSTIMESTAMP using Hibernate/Oracle11g - oracle

I have a field named end_time (of type timestamp(6)) in my Oracle 11g DB. My requirement is to fetch records which are greater than current time stamp.As I work with remote DB, I need the current time of my oracle database server.
After some research I came to know that SYSTIMESTAMP returns current time stamp of machine where DB resides.
So I just put a condition like end_time > SYSTIMESTAMP, but it does not filter records. My end-time is of type timestamp(6).
Do I have to use any conversion function? How can I do it from Hibernate? Any idea?

Can you further explain on "does not filter records", are too many rows in your result or to few?
Your condition looks absolutely ok:
CREATE TABLE mytable (ts TIMESTAMP(6));
INSERT INTO mytable (ts) VALUES (TIMESTAMP '2012-12-06 17:00:00');
INSERT INTO mytable (ts) VALUES (TIMESTAMP '2012-12-06 18:00:00');
SELECT SYSTIMESTAMP FROM DUAL;
06.12.2012 17:10:38.347629000 +01:00
SELECT * FROM mytable WHERE ts > SYSTIMESTAMP;
06.12.2012 18:00:00.000000000
SELECT * FROM mytable WHERE ts < SYSTIMESTAMP;
06.12.2012 17:00:00.000000000

Related

Oracle SQL Developer- How to force 00:00:00 hour when inserting a new DATE value

In my Oracle SQL Developer, i have a table with a column with DATE format. When i insert a new row into this table, and insert a new value in this column, it automatically suggestes me the current date with the current hour.
I would like that it automatically suggestes me current date, but with 00:00:00 hour . Is there some setting or parameter that i can set in my SQL Developer to have this result?
We can't able to insert 00:00:00 hours ... the hour value should be between 1 to 12...
we can use below query to insert 00:00:00 hours but the value will be changed to 12:00:00
INSERT INTO TABLE (DATE_COL) VALUES
( TO_DATE ('11/16/2017 00:00:00 ', 'MM/DD/YYYY HH24:MI:SS '));
It seems to me that your DATE column is set with a DEFAULT of SYSDATE. This means, for any INSERT operations which do not specify a value in your DATE column, the current date and time will populate for that row. However, if INSERT operations do specify a value in your DATE column, then the specified date value will supersede the DEFAULT of SYSDATE.
If an application is controlling INSERT operations on that table, then one solution is to ensure the application utilizes the TRUNC() function to obtain your desired results. For example:
INSERT INTO tbl_target
(
col_date,
col_value
)
VALUES
(
TRUNC(SYSDATE, 'DDD'),
5000
)
;
However, if there are multiple applications or interfaces where users could be inserting new rows into the table, (e.g. using Microsoft Access or users running INSERT statements via SQL Developer) and you can't force all of those interfaces to utilize the TRUNC() function on that column during insertion, then you need to look into other options.
If you can ensure via applications that INSERT operations will not actually reference the DATE, then you can simply ALTER the table so that the DATE column will have a DEFAULT of TRUNC(SYSDATE). A CHECK CONSTRAINT can be added for further integrity:
ALTER TABLE tbl_target
MODIFY
(
col_date DATE DEFAULT TRUNC(SYSDATE, 'DDD') NOT NULL
)
ADD
(
CONSTRAINT tbl_target_CHK_dt CHECK(col_date = TRUNC(col_date, 'DDD'))
)
;
However, if users still have the freedom to specify the DATE when inserting new rows, you will want to use a TRIGGER:
CREATE OR REPLACE TRIGGER tbl_target_biu_row
BEFORE INSERT OR UPDATE OF col_val
ON tbl_target
FOR EACH ROW
BEGIN
:NEW.col_date := TRUNC(SYSDATE, 'DDD');
END tbl_target_biu_row
;
This will take of needing to manage the application code of all external INSERT operations on the table. Keep in mind, the above trigger is also modifying the DATE column if a user updates the specified value column.

Fetch data between time difference

I want to fetch transactions happened between 10 mins . I have table with column transfer date, transfer Id but i only want to fetch those transfer id that had been generated within 10 mins
This query will give you those transfer_id which have occurred in the past 10 minutes, given that this can be determined by transfer_date.
select transfer_id
from transactions
where transfer_date > sysdate - interval '10' minute
You may try this :
select transfer_id, transfer_date from my_table where transfer_id in
(
select transfer_id from my_table
minus
select transfer_id from my_table as of timestamp systimestamp - interval '10' minute
)
i have to emphasize that your db's flashback mode should be on (if not, your dba should issue the following command):
alter database flashback on

how to use or create temp table in Oracle

I am pretty new to Oracle.
I am just stuck when i try to achieve the following logic. I am creating a sql script in oracle that will help me to generate a report. This script will run twice a day so i should't pick the same file when it runs next time.
1) run the query and save the result setand store the Order Id in the temp table when the job runs #11 Am
2) Run the query second time # 3 pm check the temp table and return the result set that's not in temp table.
Following query will generate the result set but not sure how to create a temp table and valid against when it run.
select
rownum as LineNum,
'New' as ActionCode,
ORDER_ID,
AmountType,
trun(sysdate),
trun(systime)
from crd.V_IVZ_T19 t19
where
(t19.acct_cd in
(select fc.child_acct_cd
from cs_config fc
where fc.parent_acct ike 'G_TRI_RPT'))
and t19.date>= trunc(sysdate)
and t19.date<= trunc(sysdate);
Any help much appreciated. I am not sure how to get only the timestamp also.
TEMP table is not the idea here, cause temp table data will not store the data for a long time (just for a session), you just need to create a normal table. Hope it will help you:
--- table for storing ORDER_ID for further checking, make a correct DataType, you also can add date period in the table to control expired order_ids';
CREATE TABLE order_id_store (
order_id NUMBER,
end_date DATE
);
--- filling the table for further checking
INSERT INTO order_id_store
SELECT ORDER_ID, trunc(sysdate)
FROM crd.V_IVZ_T19 t19
WHERE t19.order_id NOT IN (SELECT DISTINCT order_id FROM order_id_store)
AND t19.date>= trunc(sysdate)
AND t19.date<= trunc(sysdate);
--- delete no need data by date period, for example for last 2 days:
DELETE FROM order_id_store WHERE end_date <= trunc(SYSDATE - 2);
COMMIT;
---- for select report without already existed data
SELECT
rownum as LineNum,
'New' as ActionCode,
ORDER_ID,
AmountType,
trun(sysdate),
trun(systime)
FROM crd.V_IVZ_T19 t19
WHERE
(t19.acct_cd in
(select fc.child_acct_cd
from cs_config fc
where fc.parent_acct ike 'G_TRI_RPT'))
AND t19.order_id NOT IN (SELECT DISTINCT order_id FROM order_id_store)
AND t19.date>= trunc(sysdate)
AND t19.date<= trunc(sysdate);
I'm not sure about your "t19.date>=" and "t19.date<=", cause the close duration taking there, make that correct if it's not.

Declare YESTERDAYS Date as a variable in Oracle

I'm very new to using Oracle (I'm using TOAD 11.6), I would like to turn this code into something that would work in Oracle, how do I do it?!
declare #yesterday datetime
set #yesterday = (select cast(cast(getdate() as varchar(12)) as datetime)-1)
select *
from my_table
where disp_cret_dt >= #yesterday
Thanks in advance!!
I think you're after:
select *
from my_table
where disp_cret_dt >= trunc(sysdate-1);
That's assuming that disp_cret_dt is of datatype DATE or TIMESTAMP.
In Oracle, differences between two dates (which includes the time) are always returned as the number of days difference - and it can contain fractions of a day (eg. today at 12 noon - today at midnight = 0.5).
SYSDATE is Oracle's way of returning the current date+time.
TRUNC(dt, level) is the way you can truncate the date to whichever level you like - the default is day (which will just reset the time to midnight - 00:00), but you could do month (takes it back to the first of the month), hours etc etc.
Below is an equivalent code for oracle
declare yesterday date;
begin
select to_char(sysdate-1,'dd/mm/yyyy hh:mi:ss') into yesterday from dual;
select * into var1,var2..varn from my_table
where disp_cret_dt>=yesterday;
end;
1.Dual is temporary table in oracle which contains one column named as dummy with data type of varchar2(1). For more Refer here.
2.The SELECT INTO clause of SQL is used to retrieve one row or set of columns from the Oracle database. The SELECT INTO is actually a standard SQL query where the SELECT INTO clause is used to place the returned data into predefined variables.
If you want to return three items you have to define three variables in our pl/sql block with respective data types after applying these changes to above code it looks
declare
yesterday date;
v_item1 number;
v_item2 varchar2(11);
v_item3 date;
begin
select to_char(sysdate-1,'dd/mm/yyyy hh:mi:ss') into yesterday from dual;
select item1, item2,item3 into v_item1,v_item2,v_item3 from my_table
where disp_cret_dt>=yesterday;
Dbms_output.put_line('Item1: '||v_item1||'Item2: '||v_item2||'Item3: '||v_item3);--Displaying values
end;
Note: In the above code if your select query will returns more than one row for each yesterday value then it will throws an error. Because at a time a variable will holds one value. In that scenario we have to choose collections in oracle for more Refer here.
if you want to have "yesterday" in a seperate variable because you use it more than once in your code, assign "sysdate-1" to it:
declare
yesterday date := trunc(sysdate - 1);
begin
select * from my_table where disp_cret_dt >= yesterday;
end;

select data based on a date column

I was trying to select some data from my table using the following query:
select * from table1 where column1 = to_date('14-05-14','yy-mm-dd');
Where the column data type is DATE. I observed that, the above query won't return anything unless we modified it as,
select * from table1 where trunc(column1) = to_date('14-05-14','yy-mm-dd');
even though there are records available.
I checked the documentation for TRUNC.Can anyone please explain why this happens?
UPDATE
As per the valuable comments I think some time values may also associated with the DATE. But I cannot view/edit that time. How can I ensure there are time values associated.
Both TO_DATE and TRUNC are different. See the below example.
SQL> ALTER SESSION SET nls_date_format = 'dd/mm/yyyy hh24:mi:ss';
Session altered.
SQL> SELECT TO_DATE(SYSDATE) FROM DUAL;
TO_DATE(SYSDATE)
-------------------
28/05/2014 16:03:25
SQL> SELECT TRUNC(SYSDATE) FROM DUAL;
TRUNC(SYSDATE)
-------------------
28/05/2014 00:00:00
In Your first query to_date('14-05-14','yy-mm-dd') is comparing with the date field column1 in your table which has time values also. Whereas in Your 2nd query You are truncating the time part from table data and from Your query, that's why it's matching.
The DATE datatype stores the year (including the century), the month, the day, the hours, the minutes, and the seconds (after midnight).
TRUNC function will truncate the date to the day value, so that any hours, minutes, or seconds will be truncated off.
For more info please look at these below links
http://docs.oracle.com/cd/B28359_01/server.111/b28318/datatype.htm#CNCPT413
http://www.techonthenet.com/oracle/functions/trunc_date.php

Resources