oracle query failed with SQL error code 185 - sql

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

Related

ORA-01861: literal does not match format string ORA-06512: at "SYS.DBMS_SQL", line 1721

I need to create a lookup table of calendar for 10 years starting from 2020-01-01, but keep getting the error from the title.
Could anyone tell me how to fix the syntax?
I saw there are similar questions but I still don't know how to fix it...
Thank you in advance.
CREATE TABLE LU_DATE
(
YMD_DATE DATE NOT NULL,
YMD_ID NUMBER(8,0) NOT NULL,
YW_ID VARCHAR(7 char) NOT NULL,
YQ_ID VARCHAR(6 char) NOT NULL,
YM_ID NUMBER(6,0) NOT NULL,
Y_ID NUMBER(4,0) NOT NULL,
YMLY_ID NUMBER(6,0) NOT NULL
)
;
CREATE INDEX LU_DATEIAX ON LU_DATE
(
YMD_DATE ASC
)
;
CREATE INDEX LU_DATEIBX ON LU_DATE
(
YMD_ID ASC
)
;
INSERT INTO LU_DATE
(
YMD_DATE,
YMD_ID,
YW_ID,
YQ_ID,
YM_ID,
Y_ID,
YMLY_ID
)
SELECT
TO_DATE('20200101','YYYYMMDD') + rownum -1 AS YMD_DATE,
TO_NUMBER(TO_CHAR(TO_DATE('20200101') + rownum -1,'YYYYMMDD')) AS YMD_ID,
TO_NUMBER(TO_CHAR(TO_DATE('20200101') + rownum -1,'YYYY'))*100 +TO_NUMBER(TO_CHAR(TO_DATE('20200101') + rownum -1,'YYYYMMDD'),'ww') AS YW_ID,
TO_NUMBER(TO_CHAR(TO_DATE('20200101') + rownum -1,'YYYY'))*10 + TO_NUMBER(TO_CHAR(TO_DATE('20200101') + rownum -1,'YYYYMMDD'),'q') AS YQ_ID,
TO_NUMBER(TO_CHAR(TO_DATE('20200101') + rownum -1,'YYYYMM')) AS YM_ID,
TO_NUMBER(TO_CHAR(TO_DATE('20200101') + rownum -1,'YYYY')) AS Y_ID,
TO_NUMBER(TO_CHAR(ADD_MONTHS(TO_DATE('20200101') + rownum -1 ,-12),'YYYYMM')) AS YMLY_ID
FROM DUAL
connect by TO_DATE('20200101') + rownum -1 <= TO_DATE('20301231')
;
Use virtual columns (then you will never have a situation where your values get out-of-sync and you have two different date values in the same row):
CREATE TABLE LU_DATE
(
YMD_DATE DATE
NOT NULL,
YMD_ID NUMBER(8,0)
GENERATED ALWAYS AS (TO_NUMBER(TO_CHAR(ymd_date, 'YYYYMMDD')))
NOT NULL,
YW_ID VARCHAR2(6 char)
GENERATED ALWAYS AS (TO_CHAR(ymd_date, 'YYYYWW'))
NOT NULL,
YQ_ID VARCHAR2(5 char)
GENERATED ALWAYS AS (TO_CHAR(ymd_date, 'YYYYQ'))
NOT NULL,
YM_ID NUMBER(6,0)
GENERATED ALWAYS AS (TO_NUMBER(TO_CHAR(ymd_date, 'YYYYMM')))
NOT NULL,
Y_ID NUMBER(4,0)
GENERATED ALWAYS AS (EXTRACT(YEAR FROM ymd_date))
NOT NULL,
YMLY_ID NUMBER(6,0)
GENERATED ALWAYS AS (TO_NUMBER(TO_CHAR(ADD_MONTHS(ymd_date, -12), 'YYYYMM')))
NOT NULL
);
Then use a DATE literal:
INSERT INTO LU_DATE(YMD_DATE)
SELECT DATE '2020-01-01' + LEVEL -1
FROM DUAL
connect by DATE '2020-01-01' + LEVEL -1 < DATE '2031-01-01';
fiddle

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

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).

How to count null columns in a row?

How to count all the columns in a table that have a null value?
The table having a large number of columns and the method should iterate over the columns in a dynamic manner.
In any given row (selected by an identifier), count the null cells.
Select count(number of null value cells) where id=1 from table
e.g:
I have a table consisting of 200 columns I want to know how many null
cells does the row with id=1 have
Basically, you need to check every column in a selected row if it's null or not.
Since you have 200+ columns in your table, manual approach seems tedious, so you can automate it a little bit and construct the query dynamically by querying user_tab_columns:
-- set up
create table t1(
rid number primary key,
c1 varchar2(17),
c2 date,
c3 timestamp,
c4 number
);
insert into t1
valueS(1, 'string', null, systimestamp, null);
commit ;
-- going to use DECODE function - doesnt require type consistency.
select 'decode('||column_name||', null, 1, 0)+' as res
from user_tab_columns
where table_name = 'T1'
Result:
RES
------------------------------
decode(RID, null, 1, 0)+
decode(C1, null, 1, 0)+
decode(C2, null, 1, 0)+
decode(C3, null, 1, 0)+
decode(C4, null, 1, 0)+
And final query:
select decode(C4, null, 1, 0)+
decode(C3, null, 1, 0)+
decode(C2, null, 1, 0)+
decode(C1, null, 1, 0)+
decode(RID, null, 1, 0) as num_of_nulls
from t1
where rid = 1
Result:
NUM_OF_NULLS
--------------
2
Try this. You can pass any ID and get the number of columns with NULL value.
CREATE TABLE TEST (ID NUMBER, B VARCHAR2(20), C NUMBER, D VARCHAR2(200));
INSERT INTO TEST VALUES (1,NULL,NULL,'XX');
SELECT COUNT(NULL_COLS)
FROM (
SELECT
to_number(extractvalue(xmltype(dbms_xmlgen.getxml('SELECT CASE WHEN '||COLUMN_NAME||' IS NULL THEN 0 ELSE NULL END COL_VAL FROM '||TABLE_NAME||' WHERE ID=&VALUE')),'/ROWSET/ROW/COL_VAL')) NULL_COLS
FROM USER_TAB_COLUMNS
WHERE TABLE_NAME='TEST');

how to insert records from one table to another selecting only the common columns

i want to insert records from one table to another with common columns
desc fp_mast null? type
emp_id not null varchar2(4)
emp_nm not null varchar2(35)
typ not null varchar2(1)
flag not null varchar2(1)
st_dt not null DATE
end_dt DATE
wk_dt not null DATE
desc emp_mast null? type
emp_id not null varchar2(4)
emp_nm not null varchar2(35)
grd_cd not null varchar2(3)
desg_cd not null varchar2(2)
cost_sl not null varchar2(2)
flag not null varchar2(1)
join_dt not null DATE
resig_dt DATE
wk_dt not null DATE
i tried this query to insert the records but did not work
insert into fp_mast(emp_id,emp_nm,st_dt,end_dt,wk_dt)
select(emp_no,emp_nm,join_dt,resig_dt,wk_dt)
from emp_mast
where emp_id in ('7996','7942','5251','7999','8249','6464','8220',
'8221')
it kept showing me the following error
missing right parenthesis
thank you.
missing right parenthesis
Because you have a syntax error in the SELECT statement.
Change this:
select(emp_no,emp_nm,join_dt,resig_dt,wk_dt)
to this:
select emp_no,emp_nm,join_dt,resig_dt,wk_dt
You don't need the parenthesis for the column names in the select list. I think you got confused with the way VALUES keyword is used.

Creating table in Oracle

My requirement is that the column accno has no null value and no duplicates. The name column has no nulls and accepts only A to Z (no other like number or * $). The acctype columns is a character that allows only ( 'S' , 'C' ,'R') and the balance column has no null values. If acctype is S then balance should be >= 5000, when C the balance should be > 10000 and when it's R >= 5000.
I am trying to apply this with:
create table kcb_acc_tab
(accno varchar2(20)
constraint kcb_acc_Pk
primary key,
name varchar2(20)
constraint kcb_name_NN
Not null
constraint kcb_name_CK
check((name =upper(name)) and (name like '[(A-Z)]')),
Acctype char
constraint kcb_acctype_ck
check (acctype in('S' ,'C' ,'R')) ,
Doo timestamp
default sysdate ,
bal number(7,2) kcb_bal_NN
constraint kcb_bal_ck
check((aacctype ='S' and bal >=5000) or
(acctype = 'C' and bal >=10000) or
(acctype ='R' and bal >=5000));
This sounds like a perfect use-case for regular expressions, which I think is your intention with the like in your constraint.
I've considerably cleaned up your statement, you were missing a comma in the definition of kcb_bal_ck, and I've put the constraints at the end and added whitespace. This makes it easier, for me, to see what's going on and where any mistakes might be.
create table kcb_acc_tab(
accno varchar2(20) not null
, name varchar2(20) not null
, acctype char(1) not null -- missing size of column
, doo timestamp default sysdate not null -- missing not null
, bal number(7,2) not null
, constraint kcb_acc_pk primary key (accno)
, constraint kcb_name_ck check ( regexp_like(name, '[A-Z]', 'c' ) )
, constraint kcb_acctype_ck check ( acctype in ( 'S' ,'C' ,'R' ) )
-- acctype was spelled incorrectly.
, constraint kcb_bal_ck check( ( acctype ='S' and bal >= 5000 )
or ( acctype = 'C' and bal >= 10000 )
or ( acctype ='R' and bal >= 5000 )
) -- this parenthesis was missing
)
Here's a SQL Fiddle to demonstrate.
The main difference between this and your own is regexp_like(name, '[A-Z]', 'c' ). This ensures that the characters in the column name are solely contained within the group A-Z, that is the set of the upper-case Latin alphabet. The match_parameter 'c', specifies that the matching should be case-sensitive. The default case-sensitivity is determined by your NLS_SORT parameter, so you may not need to specify this explicitly but it's wise to do so.