Using Right() or Substr() in an UPDATE statement - sql

I have two tables :
Add_T
Add_ID NUMBER(10,0)
Add_GEOMETRY SDO_GEOMETRY
STRTN VARCHAR2(40 BYTE)
CITY VARCHAR2(45 BYTE)
STATE VARCHAR2(2 BYTE)
ZIPCODE VARCHAR2(10 BYTE)
HC_ID NUMBER(10,0)
P_ID NUMBER(10,0)
Second Table :
HC_T
HC_GEOMETRY SDO_GEOMETRY
HC_ID NUMBER(10,0)
TYPE VARCHAR2(2 BYTE)
FACY NUMBER(10,0)
COORD_X NUMBER(15,0)
COORD_Y NUMBER(15,0)
I need to update the field HC_ID of Add_T Table and I use the following SQL statement:
update add_t set hc_Id = ( SELECT HC_ID FROM HC_T
WHERE ADD_T.P_ID = HC_T.FACY AND
HCO_T.TYPE='R' ) WHERE hc_ID IS NULL and
subtr(strtn,-4,4) = "-LOC"
It doesn't work. Also, I used the right() function and also got incorrect results.
update add_t set hc_Id = ( SELECT HC_ID FROM HC_T
WHERE ADD_T.P_ID = HC_T.FACY AND
HCO_T.TYPE='R' ) WHERE hc_ID IS NULL and
RIGHT(strtn,4) = "-LOC"
Could anyone say where is the mistake?

Looks like you need the last 4 characters of a string. If so, then you'd
UPDATE add_t
SET hc_id =
(SELECT hc_id
FROM hc_t
WHERE add_t.p_id = hc_t.facy
AND hco_t.TYPE = 'R')
WHERE hc_id IS NULL
AND SUBSTR (strtn, -4) = '-LOC';
because e.g.
SQL> SELECT SUBSTR ('some string', -4) FROM DUAL;
SUBS
----
ring
SQL>
(Note also that you'd enclose strings into single quotes in Oracle, not double ones (as you tried with the "-LOC" string).

Related

ORA-06553: PLS-801:Internal Error [55018]

I want to make a function call like SELECT URUN_GETIR('test1') FROM DUAL; but i got ORA-06553: PLS-801: Internal Error [55018].
I tried ORA-06553: PLS-801: internal error [55018] when testing function returning ROWTYPE this like for ex URUN_GETIR('test1').KULUSERNAME but getting same error. It didn't work for me.Thanks in advance.
My db table :
My plsql function code :
create or replace FUNCTION URUN_GETIR(KULADI VARCHAR2)
RETURN URUN%ROWTYPE
AS
URUN_TABLO URUN%ROWTYPE;
BEGIN
SELECT * INTO URUN_TABLO FROM URUN ur WHERE ur.kulusername = KULADI;
RETURN URUN_TABLO;
END;
You can achieve your goal with using table functions. Other than that you can not call directly your function like you wanted. Here is another choice for you Use Stackoverflow Wisely
CREATE TABLE URUN
(
CREATED_BY VARCHAR2 (50 CHAR),
CREATED_DATE DATE,
UPDATED_BY VARCHAR2 (50 CHAR),
KULUSERNAME VARCHAR2 (50 CHAR),
ID NUMBER (10)
);
INSERT INTO URUN (CREATED_BY, CREATED_DATE, UPDATED_BY, KULUSERNAME, ID) VALUES('TEST1',TO_DATE('19000101','YYYYMMDD') ,'TTEST1','USER1',1);COMMIT;
INSERT INTO URUN (CREATED_BY, CREATED_DATE, UPDATED_BY, KULUSERNAME, ID) VALUES('TEST2',TO_DATE('19000102','YYYYMMDD') ,'TTEST2','USER2',2);COMMIT;
INSERT INTO URUN (CREATED_BY, CREATED_DATE, UPDATED_BY, KULUSERNAME, ID) VALUES('TEST3',TO_DATE('19000103','YYYYMMDD') ,'TTEST3','USER3',3);COMMIT;
INSERT INTO URUN (CREATED_BY, CREATED_DATE, UPDATED_BY, KULUSERNAME, ID) VALUES('TEST4',TO_DATE('19000104','YYYYMMDD') ,'TTEST4','USER4',4);COMMIT;
CREATE OR REPLACE TYPE URUN_OBJ AS OBJECT
(
CREATED_BY VARCHAR2 (50 CHAR),
CREATED_DATE DATE,
UPDATED_BY VARCHAR2 (50 CHAR),
KULUSERNAME VARCHAR2 (50 CHAR),
ID NUMBER (10)
);
CREATE OR REPLACE TYPE URUN_OBJ_TAB AS TABLE OF URUN_OBJ;
CREATE OR REPLACE FUNCTION URUN_GETIR (KULADI IN VARCHAR2)
RETURN URUN_OBJ_TAB
PIPELINED
AS
REC_OBJ URUN_OBJ;
CURSOR DATA
IS
SELECT *
FROM URUN UR
WHERE UR.KULUSERNAME = KULADI;
BEGIN
FOR REC IN DATA
LOOP
REC_OBJ :=
URUN_OBJ (REC.CREATED_BY,
REC.CREATED_DATE,
REC.UPDATED_BY,
REC.KULUSERNAME,
REC.ID);
PIPE ROW (REC_OBJ);
END LOOP;
RETURN;
END;
/
SELECT * FROM TABLE(URUN_GETIR('USER1'));
CREATED_BY CREATED_DATE UPDATED_BY KULUSERNAME ID
TEST1 1.01.1900 TTEST1 USER1 1

oracle query failed with SQL error code 185

I am trying to insert rows into a table. I m using oracle database. The query looks as follows:
INSERT INTO bv_chglogentry
SELECT DISTINCT account_coid,
To_timestamp('2018-02-01-16.04.22.428161',
'YYYY-MM-DD-HH24.MI.SSXFF'),
1,
'HP15004',
'~HP15004',
' ',
' ',
Hextoraw('00257EAB')
|| Hextoraw('0001517F0804011B'),
Hextoraw('0000000F07650368'),
' ',
0,
' ',
Trunc(To_number(To_char(To_date('2015-05-02', 'YYYY-MM-DD'), 'J'
))),
134480155,
86399,
124060520
FROM inputaccounts;
The structure of the tables looks as follows;
BV_CHGLOGENTRY
---------------
COID_ NOT NULL CHAR(26 CHAR)
TIMESTAMP_ NOT NULL TIMESTAMP(9)
PRODUCT_ID NOT NULL NUMBER(10)
ADMIN_UNIT_ID NOT NULL CHAR(8 CHAR)
OPERATOR_ID NOT NULL CHAR(10 CHAR)
OVER_ADMIN_UNIT_ID NOT NULL CHAR(8 CHAR)
OVER_OPERATOR_ID NOT NULL CHAR(10 CHAR)
CHANGE_DATE NOT NULL RAW(12 BYTE)
DESCRIPTION_ NOT NULL RAW(220 BYTE)
BUSINESS_EVENT_ID NOT NULL CHAR(32 CHAR)
OWNER_TYPE NOT NULL NUMBER(10)
OWNER_ID NOT NULL CHAR(26 CHAR)
DATE_ NOT NULL NUMBER(10)
ZONE_ NOT NULL NUMBER(10)
TIME_ NOT NULL NUMBER(10)
CHG_LOG_TYPE NOT NULL NUMBER(10)
INPUTACCOUNTS
--------------
ACCOUNT_COID NOT NULL CHAR(26)
TXN_ID NOT NULL CHAR(26)
there is a column DATE_ which is of NUMBER type. I m having problem with inserting proper data in that column. The number of columns i'm inserting is 16 which is correct in BV_CHGLOGENTRY. Can you please let me know what might be the problem with the above query.
The value you are getting in the date column, after doing insert is not incorrect. It's just converted into Julian calendar format. Because you are converting the date into Julian calendar in your select query i.e:
Trunc(To_number(To_char(To_date('2015-05-02', 'YYYY-MM-DD'), 'J')))
The value that you are getting the date column is correct i.e (2457145).
To avoid this, you can use replace() function in your SELECT query where you are doing Trunc(To_number(To_char(To_date('2015-05-02', 'YYYY-MM-DD'), 'J')))to get readable date in number format.
Sample query:
select to_number(replace('2015-08-02', '-')) from dual;
Output: 20150802

Inserting a Date data

so I have this table
RPG_RETCON (
UNIQUE_ID VARCHAR2(100 BYTE),
CONTAINER VARCHAR2(100 BYTE),
DATA_POINT_NAME VARCHAR2(100 BYTE),
SOURCE_VALUE VARCHAR2(100 BYTE),
CSS_VALUE VARCHAR2(100 BYTE),
STATUS VARCHAR2(100 BYTE)
)
And I I'm trying to insert this select statement into that table.
INSERT INTO RPG_RETCON
(SELECT A.POOL_CUSIP_ID AS UNIQUE_ID,
'1_13_1C' AS CONTAINER,
'SECU_ACTL_STLM_DT' AS COLUMN_NAME1,
TO_CHAR(A.SECU_ACTL_STLM_DT),
TO_CHAR(B.SECU_ACTL_STLM_DT),
CASE
WHEN A.SECU_ACTL_STLM_DT = B.SECU_ACTL_STLM_DT
THEN
'PASS'
ELSE
'FAIL'
END
AS STATUS
FROM POOL_1_13_1C_TRGT A
LEFT JOIN POOL_1_13_1C_CSS B ON A.POOL_CUSIP_ID = B.POOL_CUSIP_ID);
Now the problem is that SECU_ACTL_STLM_DT is a date field and when I try to do the inserts, I get an invalid number error. If I take away the TO_CHAR to just A.SECU_ACTL_STLM_DT,
B.SECU_ACTL_STLM_DT,
I get invalid month.
Note: I absolutely cannot change
SOURCE_VALUE VARCHAR2(100 BYTE)
CSS_VALUE VARCHAR2(100 BYTE)
-- Within the table structure...
They need to be VARCHAR2 Data types.
Are there any suggestions to where I can insert this select statement error free?
I think your code should work. However, I would list the columns explicitly and add date formats for the insert. Perhaps this will help:
INSERT INTO RPG_RETCON(UNIQUE_ID, CONTAINER, COLUMN_NAME1, SOURCE_VALUE, CSS_VALUE, STATUS)
SELECT A.POOL_CUSIP_ID AS UNIQUE_ID, '1_13_1C' AS CONTAINER,
'SECU_ACTL_STLM_DT' AS COLUMN_NAME1,
TO_CHAR(A.SECU_ACTL_STLM_DT, 'YYYY-MM-DD'),
TO_CHAR(B.SECU_ACTL_STLM_DT, 'YYYY-MM-DD'),
(CASE WHEN A.SECU_ACTL_STLM_DT = B.SECU_ACTL_STLM_DT
THEN 'PASS'
ELSE 'FAIL'
END) AS STATUS
FROM POOL_1_13_1C_TRGT A LEFT JOIN
POOL_1_13_1C_CSS B
ON A.POOL_CUSIP_ID = B.POOL_CUSIP_ID;
It is possible that one of the SECU_ACTL_STLM_DT columns is not a date and the comparison is failing.

ORACLE SQL ORA-22814 attribute or element value is larger than specified in type

I'm calling a function that returns a type of table.
My function receives a variable of the type varchar. Because i return a type that i created i had to make cast and multiset...
My problems is that, the select inside my multiset executes without problem outside of the function, but when i execute my function it gives me ORA-22814 attribute or element value is larger than specified in type error.
NOTE: i always run the script with the same variable value
Type OBJECT:
create or replace
TYPE Z_PEDIDO_SeB IS OBJECT(
Contrato varchar2(4000 BYTE),
DataObjectivo date,
DataObjectivoSpecified date,
Descricao varchar2(500 BYTE),
DireccaoRequerente varchar2(50 BYTE),
Empresa varchar2(50 BYTE),
Estado varchar2(50 BYTE),
GestorDDS varchar2(100 BYTE),
GestorEncomenda varchar2(30 BYTE),
Referencia varchar2(50 BYTE),
Link varchar2(500 BYTE),
ObsComite varchar2(4000 BYTE),
OutrosFornecedores varchar2(4000 BYTE),
OutrosSistAfectados varchar2(4000 BYTE),
PrincipalSistAfectado varchar2(4000 BYTE),
Prioridade varchar2(50 BYTE),
Rede varchar2(50 BYTE),
Requerente varchar2(100 BYTE),
TestesAceitacao varchar2(10 BYTE),
proj_id varchar2(50 BYTE));
Type TABLE:
create or replace
TYPE Z_TABLE_PEDIDO_SeB IS TABLE OF Z_PEDIDO_SeB;
FUNCTION:
create or replace
function Z_GetDadosCreateUpdate_SEB(
proj_id in varchar2
)
return Z_TABLE_PEDIDO_SeB as
t_dados Z_TABLE_PEDIDO_SeB;
begin
select
cast(
multiset(
--STARTS HERE WHAT I RUN WITHOUT PROBLEM OUTSIDE THE FUNCTION
select
(SELECT line_text
from long_text
where key2 = proj_id and key1 = 'contrato'
) Contrato,
NVL(SCHEDULE_FINISH,ACTUAL_FINISH) DataObjectivo,
NVL(SCHEDULE_FINISH,ACTUAL_FINISH) DataObjectivoSpecified,
pedidos.description as Descricao,
costum.direcaorequerente DireccaoRequerente,
costum.empresa Empresa,
estado.description Estado,
costum.gestordds GestorDDS,
(select recursos.description
from structure recursos,
resources,
workflow,
wf_team
where recursos.structure_code = resources.resource_code
and workflow.planning_code = projectos.structure_code
and wf_team.workflow_id = workflow.workflow_id
and wf_team.lifecycle_role_code = '868'
and wf_team.user_name = resources.LOGON_ID
and rownum = 1
) GestorEncomenda,
pedidos.structure_code ID,
(SELECT line_text
from long_text
where key2 = proj_id and key1 = 'urlcadernoreq'
) Link,
(SELECT line_text
from long_text
where key2 = proj_id and key1 = 'observacoescomite'
) ObsComite,
(SELECT line_text
from long_text
where key2 = proj_id and key1 = 'outrosfornecedores'
) OutrosFornecedores,
(SELECT line_text
from long_text
where key2 = proj_id and key1 = 'outrossistemas'
) OutrosSistAfectados,
(SELECT line_text
from long_text
where key2 = proj_id and key1 = 'principalsistema'
) PrincipalSistAfectado,
costum.prioridade Prioridade,
(SELECT rede.description
from structure rede, planning_entity proj
where proj.code88 = rede.structure_code
and proj.planning_code = proj_id
) Rede,
costum.requerente Requerente,
(SELECT rede.description
from structure rede, planning_entity proj
where proj.code89 = rede.structure_code
and proj.planning_code = proj_id
) TestesAceitacao,
projectos. structure_code proj_id
from structure projectos,
planning_entity,
structure pedidos,
structure estado,
custom_data costum
where projectos.structure_code = planning_entity.planning_code
and planning_entity.planning_code = planning_entity.ppl_code
and pedidos.structure_code = planning_entity.code31
and estado.structure_code = planning_entity.code20
and projectos.structure_code = proj_id
and costum.planning_code = proj_id
-- HERE ENDS WHAT I RUN OUTSIDE THE FUNCTION WITHOUT A PROBLEM
)
as Z_TABLE_PEDIDO_SeB)
into
t_dados
from
dual;
return t_dados;
end Z_GetDadosCreateUpdate_SEB;
What i execute:
SELECT * FROM table (PVDEV.Z_GetDadosCreateUpdate_SEB('184765'));
Error i got:
ORA-22814: valor do atributo ou elemento é superior ao especificado no tipo
ORA-06512: na "PVDEV.Z_GETDADOSCREATEUPDATE_SEB", linha 7
22814. 00000 - "attribute or element value is larger than specified in type"
*Cause: Value provided for the object type attribute or collection element
exceeded the size specified in the type declaration.
*Action: Choose another value and retry the operation.
NOTE: if i try 18476 instead of 184765 it runs with no problem. So? how did i put such restriction? Where?
NOTE: Now i'm showing the error, the real one, i'm really sorry for the mistake
I would really appreciate any answer as other's people work is waiting for my part :S. Anyway thanks in advance for any information.
If I were you I would declared the function as the pipelined one and returned values by pipe row statement. And surely get rid of CAST/multiset. For loop statement is your choice. I'm sure it will work using my piece of advice.

Comparing fields in an ORACLE view

I have an ORACLE table which includes the following fields:
FieldA1 NUMBER(10,0)
FieldA2 VARCHAR2(40 BYTE)
FieldB1 NUMBER(10,0)
FieldB2 VARCHAR2(40 BYTE)
How do I write an ORACLE view that reads from the table and if the following condition is true:
FieldA1 matches FieldB1 OR FieldA2 matches FieldB2
outputs the character 'Y' as one of the columns and if the condition above isn't true outputs 'N' ?
CREATE VIEW view_name
AS
SELECT fieldA1,
fieldA2,
fieldB1,
fieldB2,
(CASE WHEN fieldA1 = fieldB1 THEN 'Y'
WHEN fieldA2 = fieldB2 THEN 'Y'
ELSE 'N'
END) column_alias
FROM table_name
In addition to Justin Cave's answer:
Since the fields are not specified as NOT NULL, it may be convenient to interpret "fieldA match fieldB" as "fieldA = fieldB, or both fieldA and fieldB are nulls".
In this case replace fieldA = fieldB by
(fieldA = fieldB) or (fieldA is null and fieldB is null).