I have the following DDL and DML statements :
create table emp_details (
ID number(2) constraint t_pk primary key,
F_Name varchar(10) not null,
L_Name varchar(10) not null,
DOB date,
Mob_no number(10),
City varchar(10),
PIN number(5),
Gender char(1),
Designation varchar(15),
Join_Date date,
);
insert into emp_details values (01,'John','Wick','1990-07-05',9856482358,'Goa',403001,'M','SDE II', '2015-01-08');
then, I get the error of ORA-01843. So, what could be the problem?
The easiest thing to do here is to use ANSI date literals instead of strings for the dates (using strings will depend on the value of NLS_DATE_FORMAT and you don't want to play around with that if you don't have to):
INSERT INTO emp_details
VALUES
( 01, 'John', 'Wick', DATE'1990-07-05', 9856482358
, 'Goa', 403001, 'M', 'SDE II', DATE'2015-01-08');
I have to add that explicitly listing the columns into which you're inserting values is a good habit to have. Otherwise, your INSERT query will break if you or someone else adds a column from your table:
INSERT INTO emp_details
( id, f_name, l_name, dob, mob_no, city, pin, gender, designation, join_date )
VALUES
( 01, 'John', 'Wick', DATE'1990-07-05', 9856482358
, 'Goa', 403001, 'M', 'SDE II', DATE'2015-01-08');
Last, another good practice is to use VARCHAR2 instead of VARCHAR when you're working with Oracle. Currently they work the same, but Oracle "reserves the right" to change VARCHAR to meet with the ANSI standard under which NULL values and the empty string won't be the same (with VARCHAR2 they will always be the same). IOW, the behavior of VARCHAR values in Oracle can change.
It seems when you query with
select * from nls_session_parameters p where p.parameter = 'NLS_DATE_FORMAT';
you won't get YYYY-MM-DD or YYYY-DD-MM from your result of error ORA-01843.
This problem is due to inserting wrong-formatted value for date columns DOB and Join_date.
There may be two ways to prevent this error :
Assume you get DD/MM/YYYY from above query, then use 05-07-1990 for
DOB, and 08/01/2015 for Join_Date columns, respectively.
Format your values as to_date('1990-07-05','YYYY-MM-DD') for
DOB and to_date('2015-01-08','YYYY-MM-DD') for Join_Date
Related
I just began using Azure SQL Query Editor to help me learn.
I have problems formatting TEXT data when inserting data. I know I should be formatting data with "Quotations" around TEXT data, however, I continue to run into the following error: Incorrect syntax near '“'.
Example of table and dummy data:
CREATE TABLE EMPLOYEE ( eno INTEGER(11), ename TEXT(30), zip INTEGER(5) DEFAULT 47405, hdate DATE, dept TEXT (30), salary INTEGER(15,3), constraint EMPLOYEE_PK primary key (eno));
INSERT INTO EMPLOYEE VALUES (5476343153, “Stanley Keller”, , 06-Mar-99, “Sales”, 75689);
INSERT INTO EMPLOYEE VALUES (5286469147, “Sergio Murray”, , 13-Mar-92, “Marketing”, 148769);
INSERT INTO EMPLOYEE VALUES (2454152346, “Laurie Hawkins”, , 22-Mar-98, “Marketing”, 92474);
Why is this happening to me? Thank you.
Gordon is correct - single identifiers are the norm for ANSI SQL.
In SQL Server/SQL Azure, if you want to use a different quoted identifier, there is a session option to allow it if needed.
Here is the documentation on that feature which should make your original query work (non-standard SQL):
https://learn.microsoft.com/en-us/sql/t-sql/statements/set-quoted-identifier-transact-sql?view=sql-server-2017
SET QUOTED_IDENTIFIER ON
Please try the following. It worked for me.
DROP TABLE EMPLOYEE
GO
CREATE TABLE EMPLOYEE ( eno BigInt, ename NVARCHAR(30), zip INT DEFAULT 47405, hdate DATE, dept NVARCHAR(30), salary decimal(15,3), constraint EMPLOYEE_PK primary key (eno));
GO
INSERT INTO EMPLOYEE (eno, ename, hdate, dept, salary) VALUES (5476343153, 'Stanley Keller', '06-Mar-99', 'Sales', 75689);
INSERT INTO EMPLOYEE (eno, ename, hdate, dept, salary) VALUES (5286469147, 'Sergio Murray', '13-Mar-92', 'Marketing', 148769);
INSERT INTO EMPLOYEE (eno, ename, hdate, dept, salary) VALUES (2454152346, 'Laurie Hawkins', '22-Mar-98', 'Marketing', 92474);
Please take note the data types were wrong, You were treating integer like decimal and for integers the data type is int. Also you don't have to specify commas for the fields where you want the default value to take place. On the eno field I had to specify BigInt as data type since the values on the INSERT statements are to big for the integer (int) data type.
I changed double quotes for single quotes on those INSERT statements.
I have been working on my SQL database for school project and I have a problem with my idea. I am doing a car service database (all made up) and I have a column deadline of datatype date for a table order (names for both in my language - 'rok' and 'narocilo') and I am trying to insert null into some of rows where deadline isn't specified by the customer.
create table narocilo
(
id_narocila integer NOT null,
opis varchar(50),
cena integer,
status varchar(20),
datum_narocila date,
rok date
);
This is some ways i tried to insert null but none of them worked
to_date('dd-mm-yyyy', null)
to_date(null)
null
to_date('null')
So, my question is, can I insert null into a date column and if yes, how?
Thank you in advance!
You have to insert an entire row's worth of data, but as long as you have not defined that field as NOT NULL like you have above, it should be fine to insert NULL values into the date fields:
INSERT INTO narocilo(id_narocila, opis, cena, status, datm_narocila, rok)
VALUES (1001, 'something', 6, 'Open', NULL, NULL)
A very simple way to insert NULL is to leave them out of the insert altogether:
INSERT INTO narocilo (id_narocila, opis, cena, status)
VALUES (?, ?, ?, ?);
Technically, this inserts the default value. But you haven't specified one, so the default is NULL.
I want a table with the name of an employee and the year of his birth. ONLY THE YEAR IN DATE FORMAT:
CREATE TABLE EMPLOYEE(
Name VARCHAR2(50) NOT NULL,
Year_Birth DATE NOT NULL,
PRIMARY KEY (Name)
);
I want to do this:
INSERT INTO EMPLOYEE(Name, Year_Birth)
VALUES ('John Smith', 1985);
But it doesn't work (cause i'm passing a number value to a date column). I also tried this:
INSERT INTO EMPLOYEE(Name, Year_Birth)
VALUES ('John Smith', to_date('1972','YYYY') )
If i try this one i will get this:
ORA-02290: check constraint (PROJECTNAME.SYS_C0066777818) violated
Year_Birth must be a date column. Is there any way to achieve this?
I think birth year should be an INT, like this:
CREATE TABLE EMPLOYEE(
Name VARCHAR(50) NOT NULL,
Year_Birth INT NOT NULL,
PRIMARY KEY (Name)
);
Then this should work:
INSERT INTO EMPLOYEE(Name, Year_Birth)
VALUES ('John Smith', 1985);
You can create a column which is of data type int. This will then let you save the year of birth.
If the column must be of data type date then you could just save the date as the first of Jan with the relevant year (eg. for the year 2017 enter '20170101'). This will still allow you then perform date calculations on the data.
You can create a column which is of data type int. The insert statement will work.
insert into employee(Name, Year_Birth)
values ('John Smith', 1985);
I am assuming that your mysterious check constraint SYS_C0066777818 is enforcing a rule that year_birth must be 1st January.
to_date('1972','YYYY') does not give the 1st January 1972 as you might expect, it gives the first of the current month, in 1972. Who knows why, but that's the way it works. If you want 1st January 1972 then you will have to specify it explicitly, for example:
insert into employee (name, year_birth)
values ('John Smith', date '1972-01-01');
or an equivalent to_date expression that includes at least the month, or trunc(somedatevariable,'YEAR').
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');
i have a table donor_master:
create table donor_master
(
donor_id number(10) primary key not null,
dob date not null,
age number(3) not null,
gender char(1) not null,
blood_group char(3),
contact_no number(10),
address varchar(50) not null,
city varchar(10) not null,
pin number(10) not null,
state varchar(10) not null,
branch_registration_id number(5) references branch_master(branch_id)
);
when i try to insert into the table in a procedure insert_donor_master, i get "not enough values" error on compilation.
this is the procedure:
create or replace procedure insert_donor_master(
vdob donor_master.dob%type,
vage donor_master.age%type,
vgender donor_master.gender%type,
vblood_group donor_master.blood_group%type,
vcontact_no donor_master.contact_no%type,
vaddress donor_master.address%type,
vcity donor_master.city%type,
vpin donor_master.pin%type,
vstate donor_master.state%type,
vbranch_registration_id donor_master.branch_registration_id%type
)
is
begin
insert into donor_master values (sq_donor_master.nextval, vdob, vage, vgender, vblood_group, vcontact_no, vaddress, vcity, vpin, vstate, vbranch_registration_id);
commit;
end;
What is the problem?
Thanks.
Oracle hurls ORA-00947 when we specify an INSERT statement which doesn't have a value for every column in the table.
Now, the CREATE TABLE statement you posted shows a table with eleven columns. And the stored procedure code you posted shows an insert statement with eleven values in the VALUES (...) clause.
So, the explanations are:
you have a configuration management issue, and you're running the wrong version of the stored procedure or the wrong version of the table
you have a configuration management issue, and the actual structure of the table isn't what you think it is (doesn't match your CREATE TABLE script)
you aren't really getting an ORA-00947 error
Note that if you don't want to populate every row you can specify a projection of the relevant columns before the VALUES clause. For instance, if you just wanted to populate the mandatory columns you would code this:
insert into donor_master
(donor_id, dob, age, gender, address, city, pin, state )
values (sq_donor_master.nextval, vdob, vage, vgender, vaddress, vcity, vpin, vstate)
All that matters is that the number of values matches the number of columns.
The complete syntax for INSERT statements is in the documentation. enter link description hereFind out more.