SQL Oracle Date issue: ORA-01722: invalid number - sql

Wanted to have the date format as DD/MM/YYYY
I used:
ALTER SESSION SET NLS_DATE_FORMAT = 'MM/DD/YYYY';
Now when I write the line:
INSERT INTO staff Values ('ST01', 'Mrs', 'Katie', 'Elswood', '06/10/1990');
I get the error:
ERROR at line 1: ORA-01722: invalid number
Any ideas whats wrong? thanks.
or i get the error message not a valid month?
STAFF_ID NOT NULL VARCHAR2(6)
TITLE VARCHAR2(20)
FIRST_NAME VARCHAR2(20)
SURNAME VARCHAR2(20)
DOB DATE
ADDRESS VARCHAR2(50)
HOME_NO VARCHAR2(20)
MOBILE VARCHAR2(20)
EMAIL VARCHAR2(30)
NI VARCHAR2(9)
SALARY NUMBER(13,4)
EMPLOMENT_DATE DATE
BRANCH_ID VARCHAR2(3)
POSTCODE VARCHAR2(8)
POSITION VARCHAR2(30)
TOWN VARCHAR2(30)

This is never a date issue. While inserting if you are not inserting all the values then oracle would throw out an error message. Hence please explicilty mention the column names for which you are going to insert the values.
Insert into staff(staff_id,title,first_name,surname,dob) values ('ST01','Mrs','Katie','Elswood','06/10/1990');

Related

I try to insert query alphanumeric sequence in table but it is not working

I am trying to insert an alphanumeric sequence in Oracle but it is not working.
create sequence LIB start with 1 increment by 1;
select 'LIBR'||to_char(seq_no.nextval,'FM0000099') from dual;
create table addLib(
USER_ID VARCHAR2(20) PRIMARY KEY,
NAME VARCHAR2(20),
PASSWORD VARCHAR2(20),
FATHER_NAME VARCHAR2(20),
DOB DATE,
QUALIFICATION VARCHAR2(20),
DOJ DATE,
STATE VARCHAR2(20),
ADDRESS VARCHAR2(20),
PINCODE NUMBER(6));
INSERT INTO addLibrarian
values(
LIB.nextval(LIBR),
'abc',
'1234',
'xyz',
to_date('19970503','YYYYMMDD'),
'b.tech',
to_date('19970308','YYYYMMDD'),
'tanakpur',
262309);
I expect it to insert all values into the table but an error shows not enough values.
There are multiple issues:
LIB.nextval(LIBR) is not a valid syntax.
table name is not valid.
value for address is missing in VALUES clause.
Try this:
create table addLib( -- changed table name
USER_ID VARCHAR2(20) PRIMARY KEY,
NAME VARCHAR2(20),
PASSWORD VARCHAR2(20),
FATHER_NAME VARCHAR2(20),
DOB DATE,
QUALIFICATION VARCHAR2(20),
DOJ DATE,
STATE VARCHAR2(20),
ADDRESS VARCHAR2(20),
PINCODE NUMBER(6));
INSERT INTO addLib -- changed table name
values(
'LIBR' || LAPD(LIB.nextval, 7, 0), -- use something like this
'abc',
'1234',
'xyz',
to_date('19970503','YYYYMMDD'),
'b.tech',
to_date('19970308','YYYYMMDD'),
'tanakpur',
'<address>', -- add this value
262309);
Cheers!!
You have 10 columns and insert 9 values: but error show not enough values. Add the last correct value to your insertion.
This type of error can be prevented by formatting your code or use a prettifier to do it for you automatically.

How do I fix my my table so I don't get that the invalid datatype message?

I am trying to create a table in SQL and every time it I get the following error message:
ORA-00902: invalid datatype
SQL> create table BUSINESS (
2 B_IDINTEGER PRIMARY KEY,
3 B_CITYchar(20) not null,
4 B_NAMECHAR (20) NOT NULL,
5 B_CATEGORY(S) CHAR (25),
6 B_ACCTCHAR (25)
7 );
B_CITYchar(20) not null,
*
ERROR at line 3:
ORA-00902: invalid datatype
It is supposed to say table created but I don't know what is wrong with line 3.
You have several errors in your code. Try something like this:
create table BUSINESS (
B_ID INTEGER PRIMARY KEY,
B_CITY varchar2(20) not null,
B_NAME varchar2(20) NOT NULL,
B_CATEGORY varchar2(25),
B_ACCT varchar2(25)
);
Note that you should generally use variable length strings unless you know the value has a fixed length (which might be true of b_acct but is not true for b_city).

Oracle 11g : ORA-04098: trigger 'SANCIONES_TRIG' is invalid and failed re-validation

I've created a table in Oracle 11g whith an autoincrement field:
CREATE TABLE SOFOS.SANCIONES (
COD_SANCION NUMBER PRIMARY KEY,
COD_USUARIO VARCHAR2(100) NOT NULL,
FECHA_INICIO DATE NOT NULL,
FECHA_FIN DATE NOT NULL,
COD_GERENCIA NUMBER NOT NULL,
MOTIVO NUMBER NOT NULL,
COD_NOMBRE_CURSO VARCHAR2(200) NOT NULL,
MOTIVO_LEVANTAMIENTO VARCHAR2(500)
);
CREATE SEQUENCE sanciones_seq;
DROP TRIGGER sanciones_trig;
CREATE TRIGGER sanciones_trig
BEFORE INSERT ON SANCIONES
FOR EACH ROW
BEGIN
SELECT sanciones_seq.nextval
INTO :NEW.COD_SANCION
FROM dual;
END;
/
The thing is that I'm having an error like
ORA-04098: trigger 'SANCIONES_TRIG' is invalid and failed
re-validation
...what am I doing wrong??
I have followed other examples but I still have the same error.
Adding a semicolon after 'FROM dual;' solved the prblem as #krokodilko
sugested in the answers.

INSERT, SELECT, VALUES

I have table like this:-
(BL_SUBSCRIBER)
Name Null? Type
----------------------------------------- -------- -------
MSISDN NOT NULL VARCHAR2(20)
SUBSCRIBER_ID NOT NULL VARCHAR2(20)
BILLING_ID VARCHAR2(20)
SUB_TYPE NOT NULL VARCHAR2(2)
SUB_TYPE_EXTRA VARCHAR2(20)
LANGUAGE NOT NULL VARCHAR2(2)
TIME_RESTRICT_FLAG NOT NULL VARCHAR2(1)
and table like this:-
(BL_SUBSCRIBER_PACKAGE)
MSISDN NOT NULL VARCHAR2(50)
PACKAGE_SEQ NOT NULL NUMBER(8)
ID NOT NULL VARCHAR2(20)
OBJTYPE NOT NULL VARCHAR2(10)
TYPE NOT NULL VARCHAR2(10)
I want to fill the table BL_SUBSCRIBER with the MSISDN from BL_SUBSCRIBER_PACKAGE
and at the same time , I want to fill the other columns (NOT NULL) with any data,
I tried the following
insert into BL_SUBSCRIBER (
MSISDN,
SUBSCRIBER_ID,
SUB_TYPE,
LANGUAGE)
values (
select MSISDN from BL_SUBSCRIBER_PACKAGE,
sub_id_seq.nextval, //sub_id_seq is a sequence already defined.
'prepaid',
'EN')
but it gives me errors(missing expression)
SO, can anyone tell me how to copy data from one table to another and insert the default needed data (in this case it is must because it is NOT NULL).
PS:I'm Using Oracle Database
Thanks.
When you use a select as the source for an insert you don't need the values:
insert into BL_SUBSCRIBER (MSISDN,SUBSCRIBER_ID,SUB_TYPE,LANGUAGE)
select MSISDN
sub_id_seq.nextval,
'prepaid',
'EN'
from BL_SUBSCRIBER_PACKAGE;
As usual the correct syntax is documented in the manual:
https://docs.oracle.com/cd/E11882_01/server.112/e41084/statements_9014.htm#SQLRF55051
and it also has an example for this:
https://docs.oracle.com/cd/E11882_01/server.112/e41084/statements_9014.htm#SQLRF55103
try
insert into BL_SUBSCRIBER (MSISDN,SUBSCRIBER_ID,SUB_TYPE,LANGUAGE)
select
MSISDN,
sub_id_seq.nextval, //sub_id_seq is a sequence already defined.
'prepaid',
'EN'
from BL_SUBSCRIBER_PACKAGE;
Also check out #a_horse_with_no_name answer below for some useful Oracle documentation links - https://stackoverflow.com/a/27851367/1466341

ORA-01858: a non-numeric character was found where a numeric was expected? Even when the values are numbers?

This is the table
CREATE TABLE Employee
(EmpID number(5) primary key,
SIN Number(9) Not null,
LastName Varchar2(25) Not null,
FirstName Varchar2(25),
Street Varchar2(30),
City Varchar2(25),
Province Char(2),
PostalCode Varchar2(7),
JobCode Number(4) Not null,
Foreign Key(JobCode) REFERENCES Job,
IncomeTax Char(1),
BirthDate Date,
HireDate Date,
JobCodeDate Date)
TABLESPACE users;
This is the line I am trying to insert, there is only three numeric values and all of them are numbers as far as I can see.
INSERT INTO Employee VALUES(97319,516303417,'Novak','Gerry','6803 Park Ave.','Moose Jaw','SK','S6H 1X7',3000,'N','24-Aug-86','07-Jul-03','07-Jul-03');
ERROR at line 1:
ORA-01858: a non-numeric character was found where a numeric was expected
I believe the issue is with the date columns try using this syntax to_date('07-Jul-03','DD-MON-YY'):
INSERT INTO Employee
VALUES(97319,516303417,'Novak','Gerry','6803 Park Ave.','Moose Jaw','SK','S6H 1X7',3000,'N',to_date('24-Aug-86', 'DD-MON-YY'),to_date('07-Jul-03','DD-MON-YY'),to_date('07-Jul-03','DD-MON-YY'));
SQL-Fiddle: http://sqlfiddle.com/#!4/0e9df/2
alter SESSION set NLS_DATE_FORMAT = 'DD-Mon-YY';
I just had to type this in so that sql will execute the date format in my insert query's correctly
There's possibly a discrepancy between the order of fields as laid out in the INSERT statement, and the order that Oracle is expecting them. I suggest trying again using the full syntax of the INSERT (i.e. specify field names when doing the INSERT). That way, it's absolutely clear the value to field correlation being made.
So something like this:
INSERT
INTO Employee (EmpID, SIN, LastName, FirstName, Street, City, Province, PostalCode, JobCode, IncomeTax, BirthDate, HireDate, JobCodeDate)
VALUES(97319,516303417,'Novak','Gerry','6803 Park Ave.','Moose Jaw','SK','S6H 1X7',3000,'N','1986-08-24','2003-07-07','2003-07-07');