Compare two dates based on timestamp on Oracle 11g - sql

Am dealing with two tables A and B on Oracle 11g and trying to compare records (A.TRANSACTION_TIMESTAMP and B.LAST_TRANSACTION_TIMESTAMP) based on timestamp on columns with datatype DATE
Based on what i have tried, the below query does not give the expected output and returns records even when TRANSACTION_TIMESTAMP is less than LAST_TRANSACTION_TIMESTAMP
TO_CHAR(TRANSACTION_TIMESTAMP, 'DD-MON-YYYY HH24:MI:SS') >=
(SELECT TO_CHAR(LAST_TRANSACTION_TIMESTAMP, 'DD-MON-YYYY HH24:MI:SS')
FROM LAST_RAN_TIME)
Would highly appreciate if anyone can provide some inputs on comparing Dates with timestamp on Oracle 11g

Do not convert with TO_CHAR. Just compare columns directly.
The comparision is failing because you are using strings that start with the day!
TRANSACTION_TIMESTAMP >= (SELECT LAST_TRANSACTION_TIMESTAMP FROM LAST_RAN_TIME)

Related

Join 2 tables using start date time SQL Developer

I have 2 tables and, I want to create a join in SQL developer through the date times. Example, first table has a START_DATETIME 3/28/1999 15:38:00 and second table has START_DATETIME 3/28/1999 15:47:00, I want to join by date and hour only. Leaving out the min. How can I trunc the min to create a join between these tables. Please help.
Use the trunc() function. The Oracle TRUNC function returns a date truncated to a specific unit of measure. In your case, you'll want to use HH, HH12 or HH24 for the hour format.
The syntax will be trunc(a.start_datetime, 'HH24').
The Oracle documentation contain a much lengthier explanation about each of the
formats you can specify.
One option is to use to_char function with appropriate format model, e.g.
from tab_1 a join tab_2 b on to_char(a.start_datetime, 'dd.mm.yyyy hh24') =
to_char(b.start_datetime, 'dd.mm.yyyy hh24')

oracle 'ora-01843' date format error in the sql

i have a table and data as below
id joining_date
1 11-05-2020 04:15:57
I want to export from that table using date format
I am using:
select * from table where joining_date=TO_date('11-05-2020 04:15:57')
But I am getting ora-01843 error
Please any one help .. let me know the result
You can use a timestamp constant for the comparison:
where joining_date = timestamp '2020-05-11 04:15:57'
This will work even if joining_date is stored as a date and not a timestamp. dates are precise up to a second.
joining_date=TO_date('11-05-2020 04:15:57')
You are missing the required format mask:
TO_DATE ('11-05-2020 04:15:57', 'DD-MM-YYYY HH24:MI:SS')

two results when i use oracle date

i am running below sql in sql developer:
SELECT count(*)
from FTTH_AMS_DEVICE_METRICS DAT
where TRUNC(DAT.COLLECTION_TMS)>(select start_dt from ETL_JOB_CONTROL where job_name='s_m_ftth_prfrm_fact_tbl')
and TRUNC(DAT.COLLECTION_TMS)<=(select end_dt from ETL_JOB_CONTROL where job_name='s_m_ftth_prfrm_fact_tbl')
i get 38 million rows.
when i use hardcoded values in filter as below and run the query:
SELECT count(*)
from FTTH_AMS_DEVICE_METRICS DAT
where TRUNC(DAT.COLLECTION_TMS)> TO_DATE('10-14-2016','MM-DD-YYYY')
and TRUNC(DAT.COLLECTION_TMS)<= TO_DATE('10-15-2016','MM-DD-YYYY')
i am getting 12 million rows.
because i pass the date values through parameter files in informatica.
I am very much confused how to handle this and get 38 million rows when i run in Informatica.
data type for COLLECTION_TMS is timestamp and end_dt is datetime.
if you need more information on this, i will share immediately.
Thanks for your help.
If the dates are exactly as you show them, then in the second query you should have > to_date ('10-15-2016', 'mm-dd-yyyy') and <= to_date('10-16-2016', 'mm-dd-yyyy'). You are comparing to the wrong dates.
The following is speculation, informed speculation. The date data type in Oracle supports a time component. However, when you print out the date, often the time component is removed.
I am guessing that the values in start_dt and end_dt in ETL_JOB_CONTROL have time components.
If so, the following query should return at least 38 million:
select count(*)
from FTTH_AMS_DEVICE_METRICS DAT
where TRUNC(DAT.COLLECTION_TMS) > TO_DATE('10-14-2016', 'MM-DD-YYYY') and
TRUNC(DAT.COLLECTION_TMS) <= TO_DATE('10-16-2016', 'MM-DD-YYYY');
If this is the case, then just determine the full value for the two columns and use that:
select to_char(start_dt, 'YYYY-MM-DD HH24:MI:SS'),
to_char(end_dt, 'YYYY-MM-DD HH24:MI:SS')
from ETL_JOB_CONTROL;

Oracle to_timestamp

quick question on Oracle To_timestamp.
I have a two fields in a table that are both varchars
1 field contains the YYYYMMDD formatted value
the 2nd field contains the hh24mmss formatted value
Im trying to convert these two fields into a timestamp field,
expected output is DD/MM/YYYY HH24:mm:ss
like '7/23/2015 12:53:04'
however, it gaves me weird result...
like '7/15/0023 12:53:04'
seems year becomes day and day becomes year..
---------- heres my sql ----------------
select
to_date(to_char(to_date('20150723','yyyymmdd'),'yyyymmdd'),'yyyymmdd') dt,
to_char(to_date(SUBSTR('005304000',1,6), 'hh24miss'), 'hh24:mi:ss') tm,
TO_TIMESTAMP(
to_date(to_char(to_date('20150723','yyyymmdd'),'yyyymmdd'),'yyyymmdd') ||
to_char(to_date(SUBSTR('005304000',1,6), 'hh24miss'), 'hh24:mi:ss'), 'yyyy/mm/dd HH24:MI:SS' ) dttm
from dual
You have one layer to much in your conversion chain. And it will be much easier if you concatenate the two columns before converting them:
to_date(date_column||time_column, 'YYYYMMDDHH24MISS')
will return a date and a time (an Oracle DATE includes a time part).
Now that you have a proper date value, you can then format that as you want:
to_char(to_date(date_column||time_column, 'YYYYMMDDHH24MISS'), 'yyyy/mm/dd HH24:MI:SS')
As your "time" column doesn't contain milliseconds there is no real benefit in using to_timestamp.
But you should really fix your table, and store that information in a single DATE (or TIMESTAMP) column. Storing dates in varchar columns is always a very bad choice.

how to use = assignment operator with timestamp date column in oracle

I'm using timestamp in dat column in table r3. when I fire command
select dat from r3 where dat='16-nov-09';
it shows "no rows selected" but when i fire command
select dat from r3 where dat>'15-nov-09';
it shows the whole data of 16-nov-09. Tell me what is wrong in my first command or what i have to do.
Quering on oracle date columns is always confusing. The date columntype is always a datetime. Storing the current date from sysdate stores always the time component too.
There good and evil ways quering the date columns. I show and vote some.
where to_char(DAT, 'DD-MON-YYYY') = '16-NOV-2009'
where trunc(DAT) = to_date('16-NOV-2009', 'DD-MON-YYYY')
Both bad, because they do not use any index. To avoid this, you can define a function based index on the expression.
The trick of both is to cut off the time component. If time is not needed, than it is a good advise to cut off the time in INSERT and UPDATE trigger. The function based index can convert to a normal index.
where DAT between to_date('16-NOV-2009', 'DD-MON-YYYY')
and to_date('16-NOV-2009 23:59:59', 'DD-MON-YYYY HH24:MI:SS')
where DAT >= to_date('16-NOV-2009', 'DD-MON-YYYY') and DAT < to_date('16-NOV-2009', 'DD-MON-YYYY')+1
This two are always my favorites.
Its a good advice to use to_date and to_char to convert the values between string and datetime.
As DAT is timestamp you can use as below
select DAT from R3
where DAT between to_date('16-NOV-09' , 'dd-MON-yy') and to_date('16-NOV-09 23:59:59', 'DD-MON-YY hh24:mi:ss')
Timestamp has time and date components, so query
select dat from r3 where dat='16-nov-09';
will work only for records where time component is midnight: '00:00:00'
Beside formatting (to_date function), you can truncate timestamp to get only date:
select dat from r3 where trunc(dat)='16-nov-09';
Beware that this will not use index on field dat (if there is any).
TIMESTAMP and DATE are different data types in oracle and both store time components. If you really do need to store subsecond times then you use TIMESTAMP, otherwise DATE is your best choice.
The ANSI timestamp and date literal syntaxes are quite handy:
create table ts_test (ts1 timestamp);
select *
from ts_test
where ts1 > timestamp '2009-10-11 00:00:00'
/
select *
from ts_test
where ts1 > timestamp '2009-10-11 00:00:00.1'
/
select *
from ts_test
where ts1 > timestamp '2009-10-11 00:00:00.001'
/
select *
from ts_test
where ts1 = date '2009-10-11'
/
use the below format for a date field in where condition.
where to_char(DAT,'mmddyyyy') = '11152009';
In Oracle the date fields also contain a time component, so 16-nov-09 is actually midnight of Nov 16th.
Two different ways to handle this:
where to_char(DAT,'mmddyyyy') = '11152009'
as john suggested, but I like the following version more:
where trunc(dat) = to_date ('11152009', 'mmddyyyy')
TRUNCfor a date "removes" the time component (or to be more specific, truncates it to midnight), and to_date is the proper way to construct a date value in Oracle SQL. (I prefer to do the comparisons in the right domain - DATEs as in the second example- over another - STRINGs as in the first example. With strings you may run into some weird month issues, sorting is easier in dates etc.)
Just to add to it , An easy way out when you are not bothered about the time-stamp but just want to compare the date is to use the 'like' operator.
for example
select dat from r3 where dat LIKE '16-nov-09%'
will give you desired output.