Oracle - CAST(row_number) query - oracle

SELECT NOABSEN,TANGGAL,JAM,
'Jam' + CAST(ROW_NUMBER() OVER (PARTITION BY NOABSEN ORDER BY NOABSEN desc) as VARCHAR(20))
as ColumnsSequence
From HRD_ABS_FINGER_DIVISI;
when the command is run appears an error like this:
ORA-01722: invalid number
01722. 00000 - "invalid number"
*Cause: The specified number was invalid.
*Action: Specify a valid number.

Here:
'Jam' + CAST(ROW_NUMBER() OVER(..) AS VARHCAR(20))
Unlike other databases (namely, SQL Server), the addition operator in Oracle really means that: addition. So as you are actually trying to add two stings, so Oracle attempts to convert them to numbers - which fails for literal string 'Jam'.
Likely, that you want string concatenation (||) rather than an addition (+):
SELECT
NOABSEN,
TANGGAL,
JAM,
'Jam' || CAST(ROW_NUMBER() OVER (PARTITION BY NOABSEN ORDER BY NOABSEN desc) as VARCHAR(20)) as ColumnsSequence
From HRD_ABS_FINGER_DIVISI;

Related

ORA-01722: invalid number 01722. 00000 - "invalid number" *Cause: The specified number was invalid. *Action: Specify a valid number

I am new to Oracle SQL Developer, and today while running this
select r.id, r.date, it.group, it.comment, it.item, it.remark, r.summary,
substr (it.remark, instr(it.remark,'ABC')+8,7 ) as label1,
cast(substr (it.remark, instr(it.remark,'-')+1,3 ) as integer) as label2
from it_table it
inner join sp_table sp on sp.id = substr (it.remark, instr(it.remark,'ABC')+8,7 ) and sp.label_id = cast(substr (it.remark, instr(it.remark,'-')+1,3 ) as integer)
inner join sq_table sq on sq.id = sp.id
where it.date > '01-jan-2020' and it.remark like '%ABC%' and it.group= 'O'
order by sp.id, it.id;
it caught the error:
ORA-01722: invalid number
01722. 00000 - "invalid number"
*Cause: The specified number was invalid.
*Action: Specify a valid number.
I think the problem lies with the extraction as in row 3 (cast(substr (it.remark, instr(it.remark,'-')+1,3 ) as integer)), where I need to convert a string into a number using cast.
According to doc, the error occurs when an attempt is made to convert a character string into a number, and the string cannot be converted into a valid number.
So, I tried replacing:
cast(substr (it.remark, instr(it.remark,'-')+1,3 ) as integer)
with
to_number(substr (it.remark, instr(it.remark,'-')+1,3 ))
and even tried to_char but didn't work. However, the original script seems to work fine in sandbox database. I am wondering why this is happening. Any help is greatly appreciated.
Update:
Sample data it:
ID DATE NAME GROUP REMARK COMMENT ...
100 20-10-08 AABC X ACS LOCATION 1 - ABC IDD x105213-1
​101 20-10-08 AxB Y MN LOCATION 8 - ABC IDD x105244-2
...
Sample data sp:
ID DATE NAME GROUP label_id
105213 20-10-08 AABC X 1
​105244 20-10-08 AxB Y 2
...
It turns out that the error was caused by having 2 - in remark which lead to ambiguity and I just need the second one.
New question then:
How do I extract the last - in the value to join with another value in the other column?
Use cast with default null on conversion error to avoid exception and investigate the cause of the failed conversion.
Example
with dt as
(select '001' remark from dual union all
select ' 2' from dual union all
select 'OMG' from dual)
select substr(remark,1,3) txt,
cast (substr(remark,1,3) as INT default null on conversion error) num
from dt;
TXT NUM
--- ----------
001 1
2 2
OMG

ORA-00936: missing expression Solution- Convert function

when doing this query in oracle I get the error
ORA-00936: missing expression
00936. 00000 - "missing expression"
If I run the query to the from of it gives me results, then I deduce that the problem comes from where, however, I cannot identify what it is
SELECT FECHADOC, FECHACONT, CLASEDOC, SOCIEDAD, MONEDA, TIPOCAMBIO, PERIODO,
REFERENCIA, TEXTOCAB, ID_REGISTRO
FROM ESQUEMA.TABLE
WHERE CONVERT(CHAR(8),20211231,112) <= CONVERT(CHAR(8),DATEADD(DAY,-90,GETDATE()),112)
I already used:
WHERE CONVERT( TO_CHAR(8),20201231,112) <=
(CONVERT(TO_CHAR(8),DATEADD(DAY,-90,GETDATE()),112) )
and it keeps giving me an error
If this really is Oracle, then dateadd and getdate aren't Oracle functions. Look like MS SQL Server ones. Also, table is reserved word for tables, you can't name a table (or any other object) table.
Anyway: looks like this is what you might be looking for:
SELECT FECHADOC, FECHACONT, CLASEDOC, SOCIEDAD, MONEDA,
TIPOCAMBIO, PERIODO, REFERENCIA,
TEXTOCAB, ID_REGISTRO
FROM ESQUEMA.TABLE
where to_date('20211231', 'yyyymmdd') <= trunc(sysdate) - 90;

Getting error in vertica:For 'IN', types varchar and int are inconsistent DETAIL: Columns: unknown and unknown

I am trying to execute below query in vertica:
select case
when to_char(to_date('02-15-2017','mm-dd-yyyy'),'mm')in(1,2,3,4,5,6,7,8,9,10) then
(select to_char(add_months(trunc(to_date('02-15-2017','mm-dd-yyyy'),'year'),-2),'mm-dd-yyyy') from dual);
else
(select to_char(add_months(trunc(to_date('02-15-2017','mm-dd-yyyy'),'year'),10),'mm-dd-yyyy') from dual)
end ett from dual;
Query is getting expected results in oracle but in Vertica Getting error
:For 'IN', types varchar and int are inconsistent DETAIL: Columns:
unknown and unknown.
Expected Result is fiscal Start date as 1st Nov 2016.
The base problem is that you are comparing a string to a number in your IN() predicate. (TO_CHAR() with a list of numbers). While Oracle (which your used syntax suggests) often implicitly converts data types to make a comparison happen, Vertica, like most other more ANSI compliant databases, is much stricter.
And you could have written your statement in a more concise way:
You don't need the FROM dual clause in Vertica (even if it implicitly adds it internally)
You wouldn't even have to nest the SELECTS in Oracle; it's enough to SELECT the whole CASE expression.
You could formulate the date literals much easier, by using the ISO format and preceding that string with the DATE keyword.
So, in general, I'd have written it like so:
SELECT
CASE
WHEN MONTH(DATE '2017-02-15') <= 10
THEN TO_CHAR(ADD_MONTHS(TRUNC(DATE '2017-02-05','YEAR'), -2),'mm-dd-yyyy')
ELSE TO_CHAR(ADD_MONTHS(TRUNC(DATE '2017-02-05','YEAR'), 10),'mm-dd-yyyy')
END AS ett
;

Why is the plus sign (+) before a string value accepted in Oracle?

Ok, this is perhaps a quirkiness of the Oracle parser.
The following query works. Note the + before 'Y' on the last line.
SELECT *
FROM (SELECT 'Y' AS field FROM DUAL
UNION ALL
SELECT 'X' AS field FROM DUAL) t
WHERE t.field = +'Y'
Why is the Oracle parser accepting this? For a second I thought it was because of the old outer join syntax but in that syntax the + is surrounded with parentheses.
This works as well:
select +'Y1' from dual;
and this:
select 'A' || + 'Y1' from dual;
This works (oracle converts the string to a number):
select -'1' from DUAL;
but not this ([Error] Execution (223: 9): ORA-01722: invalid number
):
select -'A' from DUAL;
I wonder why the + can be used before a varchar2 value. The Arithmetic Operators section doesn't mention specific rules that would apply to string values.
The unary + operator is defined as identity See the Table 4-1 "SQL Operator Precedence" in About SQL Operators.
Also:
select + date '2015-01-01' from dual;
January, 01 2015 00:00:00
Edited to add.
"Identity" being to return its argument. For another example from a different language see Clojure's identity function. Wikipedia has a page for "identity function".

Getting Error with Date Format in To_Char

I am getting error in the second script. Please explain why i am getting error in the second script.
select count(*) from LCL_SHR_IncidentIntegrationInt where
externalsystem = 'IPSOFT'
and (to_char(sysdate,'YYYYMMDDHH24MISS')-to_char(fn_adjusted_date(CREATE_DATE),'YYYYMMDDHH24MISS')) > 180;
O/P : 122797
select count(*) from LCL_SHR_IncidentIntegrationInt where
externalsystem = 'IPSOFT'
and (to_char(sysdate,'DD-MM-YYYY HH24:MI:SS')-to_char(fn_adjusted_date(CREATE_DATE),'DD-MM-YYYY HH24:MI:SS')) > 180;
O/P : ORA-01722: invalid number
01722. 00000 - "invalid number"
*Cause: The specified number was invalid.
*Action: Specify a valid number.
The first query works because Oracle is able to implicitly cast the characters as number and compare with 180. The other one doesn't because the : and - cannot be implicitly cast to number.
You should use date and time functions for such cases like timestampdiff or datediff. More functions here - https://docs.oracle.com/cd/E17952_01/refman-5.1-en/date-and-time-functions.html

Resources