Max Timestamp not giving correct result - oracle

I have a simple code to display latest error msg based on timestamp:
SELECT
line_item || ': ' || error_msg as RejectionMsg
FROM reqs
WHERE reqs_number = 'XXXXXXX'
and rqj_timestamp = (select max(rqj_timestamp) from reqs
WHERE reqs_number = 'XXXXXX' )
My data is something like :
rqj_timestamp line_item Error_msg
08-MAY-2009 14:00:04 8928 INVALID (RC4C) E
08-MAY-2009 14:00:04 8929 INVALID (R4CO) EY0
05-AUG-2013 00:13:42 11760 OO_USR_1 - NO_DATA_FOUND:No Data found for REQUEST
05-AUG-2013 00:13:42 11761 OO_USR_1 - NO_DATA_FOUND:No Data found for REQUEST
05-AUG-2013 00:13:42 11762 OO_USR_1 - NO_DATA_FOUND:No Data found for REQUEST
05-AUG-2013 00:14:59 11763 OO_USR_1 - NO_DATA_FOUND:No Data found for REQUEST
06-AUG-2013 06:55:59 11807 OO_45_ERROR_REGION_DERIV
06-AUG-2013 06:55:59 11808 OO_45_ERROR_REGION_DERIV
06-AUG-2013 06:55:59 11809 OO_45_ERROR_REGION_DERIV
My query is giving me output for 08-MAY-2009 14:00:04 time-stamp instead of the 06-AUG-2013 06:55:59 time-stamp.
3: INVALID (RC4C) E
3: INVALID (R4CO) EY0
Any idea where I am going wrong in this?OR how i can improve my query.. if i remove 08-MAY-2009 14:00:04 rows, it works perfectly fine.
Thanks in advance for help.

I would assuem your timestamp is not a date or similar data type, but a character data type like nvarchar2. Change you table to the proper data type.
A working but less clean solution would be to use something like
cast((select max(cast(rqj_timestamp as date)) from reqs) as nvarchar2)
which might depend on national language settings etc.
You would use the character data type that the column has in the outer cast.

Try something like (untested):
select *
from (
select r.*,
row_number() over (partition by reqs_number order by rqj_timestamp desc nulls last) rnum
from reqs r
where reqs_number = 'XXXXXXX'
)
where rnum = 1;
Also, the assumption is the "rqj_timestamp" is actually a timestamp (or at least a date).

Thanks for pointing out to check the datatype..turns out it was varchar, adding to_date(rqj_timestamp,'DD-MON-YYYY HH24:MI:SS') worked.

Related

Multi values search from one field PLSQL

I'm creating a report in PLSQL.
The report is working fine and additionally, I need to add when users search multiple values from one field separating by ;. It should select all the data that he entered.
Example: Salesman - Amanda; Michelle; Sharmain
Then it should select data regarding amenda, michelle and sharmain.
My code only returns data for one value only.
please refer this line from the code and ifsapp.Customer_Order_API.Get_Salesman_Code(i.order_no) LIKE '&Salesman'
where i.catalog_group='FPMB'
and i.order_no like 'M%'
and ((i.invoice_date between to_date( '&From_Date', 'YYYY/MM/DD' ) and to_date( '&To_Date', 'YYYY/MM/DD' ) ) or ('&From_Date' is null and '&To_Date' is null))
and t.source_ref1=i.order_no
and (t.source_ref3=i.release_no)
and (t.source_ref2=i.line_no)
and i.contract=t.contract
and t.transaction_code='OESHIP'
and t.qty_reversed=0
and t.source_ref1=i.order_no
and t.serial_no <> '*'
and t.cost<>0.00
and i.order_no LIKE '&Order_No
and ifsapp.Customer_Order_API.Get_Salesman_Code(i.order_no) LIKE '&Salesman'
You can use hierarchy query as follows:
ifsapp.Customer_Order_API.Get_Salesman_Code(i.order_no) IN
(SELECT TRIM(REGEXP_SUBSTR('&Salesman','[^;]+',1,LEVEL))
FROM DUAL CONNECT BY TRIM(REGEXP_SUBSTR('&Salesman','[^;]+',1,LEVEL)) IS NOT NULL)

Having issue with extracing date from CLOB data

Hi I am having issue when extracting fields from CLOB data. For one record I am not getting desired output.
The record is as below:
{1:F014243107336}{2:O2021216200422XXX24563}{3:{108:O2020}{121:2c02a452-5}{433:HIT}}{4:
:4A:SEC:20200901
:4B:FC5253
:4C:20042000,
:4D:XXXXXXX
:4E:RXX
:4F:RXXXX
-}{5:{CHK:87D1003B01F7}{TNG:}}{S:{SAC:}{COP:S}}<APSECSIGN>FS3sfasdfg!==</APSECSIGN>?
I want to extract data from tag :4A: into REF_NUMBER.
I am using below SQL to get the data.
NVL(TRIM(TRANSLATE(REGEXP_REPLACE(REGEXP_SUBSTR(dbms_lob.substr(CLOB, 4000, 1 ), ':4A.?:[^:]+(:|-\})'), ':20.?:([^:]+)(:|-\})', '\1'),CHR(10)||CHR(13), ' ')),' ') AS REF_NUMBER
the output I am getting is "SEC". However I want to see output as SEC:20200901.
Can any one suggest what I am missing in my query or provide me correct query.
A general suggestion. Why don't you have your data stored as JSON ? Because, JSON related functions are very fast when compared to others. And then your problem becomes quite easy.
However to answer your question:
with inputs (str) as
(
select to_clob(q'<
{1:F014243107336}{2:O2021216200422XXX24563}{3:{108:O2020}{121:2c02a452-5}{433:HIT}}{4:
:4A:SEC:20200901
:4B:FC5253
:4C:20042000,
:4D:XXXXXXX
:4E:RXX
:4F:RXXXX
-}{5:{CHK:87D1003B01F7}{TNG:}}{S:{SAC:}{COP:S}}<APSECSIGN>FS3sfasdfg!==</APSECSIGN>?
>') from dual
)
select str, regexp_substr(str,'SEC:\d+',1,1,'n') as val
from inputs;
Output:
Updated
If you know the date is always going to be 8 digits after the :4A: tag, you can use REGEXP_SUBSTR to get the value you need. Combining it with DBMS_LOB.SUBSTR removes the tag and converts it to a string.
SELECT DBMS_LOB.SUBSTR ((REGEXP_SUBSTR (clob_val, ':4A:.*\d{8}')), 4000, 5)
FROM (SELECT EMPTY_CLOB ()
|| '{1:F014243107336}{2:O2021216200422XXX24563}{3:{108:O2020}{121:2c02a452-5}{433:HIT}}{4:
:4A:SEC:20200901
:4B:FC5253
:4C:20042000,
:4D:XXXXXXX
:4E:RXX
:4F:RXXXX
-}{5:{CHK:87D1003B01F7}{TNG:}}{S:{SAC:}{COP:S}}<APSECSIGN>FS3sfasdfg!==</APSECSIGN>?' AS clob_val
FROM DUAL);

compare 13digit (millisecond) unix timestamp with date in oracle

A database column (VARCHAR2 datatype) stores the date/time as 13 digit (milliseconds
) unixtimestamp format. Now when I want to compare the column with a oracle date (in question), The error thrown as 'invalid number'
I tried both ways,
converting the 13digit number to Date and compare with the date in question like below. The expressions seems valid as they are printed in select query, but if i include in the where part, it throws 'invalid number'
Here 'value' is 13th digit unixtimestamp column of VARCHAR2 datatype.
select
TO_DATE('1970-01-01', 'YYYY-MM-DD') + value/86400000,
TO_DATE('2014-04-21', 'YYYY-MM-DD')
from dummytable
-- where and TO_DATE('1970-01-01', 'YYYY-MM-DD') + value/86400000 > TO_DATE('2014-04-21', 'YYYY-MM-DD')
converting the date in question to 13digit unixtimestamp and comparing with the database column.The expressions seems valid as they are printed in select query, but if i include in the where part, it throws 'invalid number'
.
select
value,
(to_date('2013-04-21', 'YYYY-MM-DD') - to_date('1970-01-01', 'YYYY-MM-DD')) * (1000*24*60*60)
from dummytable
-- where value > ((to_date('2013-04-21', 'YYYY-MM-DD') - to_date('1970-01-01', 'YYYY-MM-DD')) * (1000*24*60*60))
any pointers? Thanks in advance.
[EDIT- 1 day later] I see the problem now. There are some data (other rows) for the 'value' column that are non-numeric. But I have another column say field, where always field='date' return value as 13 digit timestamp. Now I think when 'where' condition executes, although the field='date' is in the condition, it is still validating the other values for 'value' which are non-numeric. Is there a way to avoid this ?
Your code works just fine. The problem is in your data. Some of your values is not a number.
create table test
(value varchar2(13));
insert into test(value) values('2154534689000');
--insert into test(value) values('2 54534689000');
select TO_DATE('1970-01-01', 'YYYY-MM-DD') + value/86400000
from test
where TO_DATE('1970-01-01', 'YYYY-MM-DD') + value/86400000 > TO_DATE('2014-04-21', 'YYYY-MM-DD');
This code works fine. But if you uncommented the second insert, you would get exactly the same invalid number error as you get.
UPD. Allan gave you a nice hint, but i feel that it can be good to explain you a bit about views. The fact that you select from a view CAN make a difference. A view is not stored somewhere physically, when you select from it, it is just "added to your query". And then Oracle Query Optimizer starts working. Among other things, it can change the order in which your where predicates are evaluated.
For example, your the view query can have a line where value is not null and it would normally show only 'good' values. But if your query has a predicate where to_date(value,'ddmmyyyy') > sysdate, Oracle can decide to evaluate your predicate earlier, because Oracle predicts that it would "cut off" more rows, thus making the whole query faster and less momery consuming. Of course, execution will crash because of an attempt to convert a null string to date.
I believe, that Allan in his answer that he gave a link to, gave a great way to solve this problem: "wrapping" your query in a subquery that Oracle can't "unwrap":
select value
from
(select value
from my_view
where rownum > 0)
where to_date(value,'ddmmyyyy') > sysdate
Hope that helps.

SQL Navigator throws 'ORA-01834: Not a valid month' but query runs in other applications

I have stocked in this error many times but know I have no way to avoid and I have to get rid of it.
Sometimes I do run a query in SQL Navigator 6.1 XPert Edition and it throws:
ORA-01843: Not a valid month
But if I run this same query in same database but in other application(ie Aqua Data Studio) it works fine. It's just in isolated cases.
It may be some config problem?
EDIT: This query has that problem:
select
quantity dias_a_vencer
, estab
, initcap (planejador) planejador
, atributo2 fabrica
, mrp.item montagem
, initcap (descricao) des_montagem
, mrp.nro_docmto num_of
, initcap (mrp.fornecedor) cliente
, mrp.project_number projeto
, initcap (comprador) processista
, trunc (mrp.data_inicio) data_inicio
from etlt_mrp_exceptions mrp
where
mrp.compile_designator = 'ENGI'
and mrp.dt_coleta > sysdate - 50
and estab = '179' -- PARAMETRO ESTAB FILTRO
and atributo2 = '11' -- PARAMETRO FABRICA FILTRO
and nvl (mrp.quantity, 0) > 0
and dt_coleta = '05/12/2011' -- parametro do grafico acima
and initcap (planejador) = 'Maria Cristina Da Cruz Costa' -- parametro do grafico acima
order by quantity
, des_montagem
To make your query fail-safe in all environments, you have to change this line:
and dt_coleta = '05/12/2011'
to
and dt_coleta = to_date('05/12/2011', 'DD/MM/YYYY')
Assuming that you meant December 5th, and not May, 12th.
Btw: what datatype are the columns estab and atributo2. If those are numbers you should remove the single quotes around the parameters. That is another "implicit" data conversion that would e.g. prevent the usage of an index on those columns.
Always specify a date format, never assume it or use default formats. For example:
insert into mytable (mydate) values (to_date('02/28/2011', 'MM/DD/YYYY'));
Unfortunately, even using TO_DATE() does not guarantee success. (But I would strongly recommend that you are always aware of the date format in play.) For instance using SQL*Developer, this works:
alter session set nls_date_format='yyyy-mon-dd hh24:mi:ss.ddd';
select * from nns.nns_logGER WHERE LOG_DATE >= '2014-jul-30 14:47:16.211';
but this fails with "ORA-01834: day of month conflicts with Julian date" error:
alter session set nls_date_format='yyyy-mon-dd hh24:mi:ss.ddd';
select * from nns.nns_logGER WHERE LOG_DATE >= '2014-jul-30 14:47:16.210';
Notice that I changed only the last digit. And using to_date did not help:
select * from nns.nns_logGER WHERE LOG_DATE >=
to_date('2014-jul-30 14:47:16.210','yyyy-mon-dd hh24:mi:ss.ddd');
fails in the same way.
I wish there were better news, but I think this must be some internal problem.

PL/SQL query IN comma deliminated string

I am developing an application in Oracle APEX. I have a string with user id's that is comma deliminated which looks like this,
45,4932,20,19
This string is stored as
:P5_USER_ID_LIST
I want a query that will find all users that are within this list my query looks like this
SELECT * FROM users u WHERE u.user_id IN (:P5_USER_ID_LIST);
I keep getting an Oracle error: Invalid number. If I however hard code the string into the query it works. Like this:
SELECT * FROM users u WHERE u.user_id IN (45,4932,20,19);
Anyone know why this might be an issue?
A bind variable binds a value, in this case the string '45,4932,20,19'. You could use dynamic SQL and concatenation as suggested by Randy, but you would need to be very careful that the user is not able to modify this value, otherwise you have a SQL Injection issue.
A safer route would be to put the IDs into an Apex collection in a PL/SQL process:
declare
array apex_application_global.vc_arr2;
begin
array := apex_util.string_to_table (:P5_USER_ID_LIST, ',');
apex_collection.create_or_truncate_collection ('P5_ID_COLL');
apex_collection.add_members ('P5_ID_COLL', array);
end;
Then change your query to:
SELECT * FROM users u WHERE u.user_id IN
(SELECT c001 FROM apex_collections
WHERE collection_name = 'P5_ID_COLL')
An easier solution is to use instr:
SELECT * FROM users u
WHERE instr(',' || :P5_USER_ID_LIST ||',' ,',' || u.user_id|| ',', 1) !=0;
tricks:
',' || :P5_USER_ID_LIST ||','
to make your string ,45,4932,20,19,
',' || u.user_id|| ','
to have i.e. ,32, and avoid to select the 32 being in ,4932,
I have faced this situation several times and here is what i've used:
SELECT *
FROM users u
WHERE ','||to_char(:P5_USER_ID_LIST)||',' like '%,'||to_char(u.user_id)||',%'
ive used the like operator but you must be a little carefull of one aspect here: your item P5_USER_ID_LIST must be ",45,4932,20,19," so that like will compare with an exact number "',45,'".
When using it like this, the select will not mistake lets say : 5 with 15, 155, 55.
Try it out and let me know how it goes;)
Cheers ,
Alex
Create a native query rather than using "createQuery/createNamedQuery"
The reason this is an issue is that you cannot just bind an in list the way you want, and just about everyone makes this mistake at least once as they are learning Oracle (and probably SQL!).
When you bind the string '32,64,128', it effectively becomes a query like:
select ...
from t
where t.c1 in ('32,64,128')
To Oracle this is totally different to:
select ...
from t
where t.c1 in (32,64,128)
The first example has a single string value in the in list and the second has a 3 numbers in the in list. The reason you get an invalid number error is because Oracle attempts to cast the string '32,64,128' into a number, which it cannot do due to the commas in the string.
A variation of this "how do I bind an in list" question has come up on here quite a few times recently.
Generically, and without resorting to any PLSQL, worrying about SQL Injection or not binding the query correctly, you can use this trick:
with bound_inlist
as
(
select
substr(txt,
instr (txt, ',', 1, level ) + 1,
instr (txt, ',', 1, level+1) - instr (txt, ',', 1, level) -1 )
as token
from (select ','||:txt||',' txt from dual)
connect by level <= length(:txt)-length(replace(:txt,',',''))+1
)
select *
from bound_inlist a, users u
where a.token = u.id;
If possible the best idea may be to not store your user ids in csv! Put them in a table or failing that an array etc. You cannot bind a csv field as a number.
Please dont use: WHERE ','||to_char(:P5_USER_ID_LIST)||',' like '%,'||to_char(u.user_id)||',%' because you'll force a full table scan although with the users table you may not have that many so the impact will be low but against other tables in an enterprise environment this is a problem.
EDIT: I have put together a script to demonstrate the differences between the regex method and the wildcard like method. Not only is regex faster but it's also a lot more robust.
-- Create table
create table CSV_TEST
(
NUM NUMBER not null,
STR VARCHAR2(20)
);
create sequence csv_test_seq;
begin
for j in 1..10 loop
for i in 1..500000 loop
insert into csv_test( num, str ) values ( csv_test_seq.nextval, to_char( csv_test_seq.nextval ));
end loop;
commit;
end loop;
end;
/
-- Create/Recreate primary, unique and foreign key constraints
alter table CSV_TEST
add constraint CSV_TEST_PK primary key (NUM)
using index ;
alter table CSV_TEST
add constraint CSV_TEST_FK unique (STR)
using index;
select sysdate from dual;
select *
from csv_test t
where t.num in ( Select Regexp_Substr('100001, 100002, 100003 , 100004, 100005','[^,]+', 1, Level) From Dual
Connect By Regexp_Substr('100001, 100002,100003, 100004, 100005', '[^,]+', 1, Level) Is Not Null);
select sysdate from dual;
select *
from csv_test t
where ('%,' || '100001,100002, 100003, 100004 ,100005' || ',%') like '%,' || num || ',%';
select sysdate from dual;
select *
from csv_test t
where t.num in ( Select Regexp_Substr('100001, 100002, 100003 , 100004, 100005','[^,]+', 1, Level) From Dual
Connect By Regexp_Substr('100001, 100002,100003, 100004, 100005', '[^,]+', 1, Level) Is Not Null);
select sysdate from dual;
select *
from csv_test t
where ('%,' || '100001,100002, 100003, 100004 ,100005' || ',%') like '%,' || num || ',%';
select sysdate from dual;
drop table csv_test;
drop sequence csv_test_seq;
Solution from Tony Andrews works for me. The process should be added to "Page processing" >> "After submit">> "Processes".
As you are Storing User Ids as String so You can Easily match String Using Like as Below
SELECT * FROM users u WHERE u.user_id LIKE '%'||(:P5_USER_ID_LIST)||'%'
For Example
:P5_USER_ID_LIST = 45,4932,20,19
Your Query Surely Will return Any of 1 User Id which Matches to Users table
This Will Surely Resolve Your Issue , Enjoy
you will need to run this as dynamic SQL.
create the entire string, then run it dynamically.

Resources