ORA-00907: missing right parenthesis error in oracle of a sql commandd - sql

I tried plenty changes but still i get the same error - ORA-00907: missing right parenthesis
Insert into EMPLOYEES_14
(employee_id,
first_name,
last_name,
email,
phone_number,
hire_date,
job_id,
salary,
commission_pct,
manager_id,
department_id,
dn)
Values
('101',
hazel,
page,
hazel#gmail.com,
'123456789',
to_date ('2019-01-01 08 00 00' 'YYYY-MM-DD HH-MM-SS '),
hr101,
'10201.00',
'20.00' ,
'123456' ,
'1001' ,
hazel is a good intern )
;

The following is missing a comma:
to_date ('2019-01-01 08 00 00' 'YYYY-MM-DD HH-MM-SS '),
it should read:
to_date ('2019-01-01 08 00 00' , 'YYYY-MM-DD HH-MM-SS'),
(and I don't believe you need the trailing space after SS either)
and you need quotes for all the string literals
Insert into EMPLOYEES_14
(employee_id,
first_name,
last_name,
email,
phone_number,
hire_date,
job_id,
salary,
commission_pct,
manager_id,
department_id,
dn)
Values
(101,
'hazel',
'page',
'hazel#gmail.com',
'123456789', -- assume this is a string
to_date ('2019-01-01 08 00 00', 'YYYY-MM-DD HH-MM-SS'),
'hr101',
10201.00,
20.00 ,
123456 ,
1001 ,
'hazel is a good intern')
;

You should introduce date/time constants using the date or timestamp keyword. The other problems are missing single quotes:
Values ('101', 'hazel', 'page', 'hazel#gmail.com', '123456789',
timestamp '2019-01-01 08:00:00',
'hr101',
'10201.00', '20.00', '123456', '1001',
'hazel is a good intern'
)
Note that number values should not use single quotes. So if values such as 10201.00 are really numbers, drop the single quotes:
Values (101, 'hazel', 'page', 'hazel#gmail.com', 123456789,
timestamp '2019-01-01 08:00:00',
'hr101',
10201.00, 20.00, 123456, 1001,
'hazel is a good intern'
)

Related

sql oracle top 3 visited countries query from multiple tables

I designed a ticket booking system for flights. After I made some inserts, I want to write a query to show top 3 visited countries.
This is what I wrote:
select * from ( select flightnumber, count(*) as NumberOfFlights from ticket group by flightnumber order by NumberOfFlights desc) where rownum < 4;
My question is how to select Country column from Airport table such that to the current query result columns "FlightNumber" and "NumberOfFlights" to add "Country" name which corresponds to FlightNumber.
One example:
Airport table
insert into airport(icao_code, name, city, country) values ('PJAN', 'IS Airport', 'Iasi', 'Romania');
insert into airport(icao_code, name, city, country) values ('SBJN', 'SB Airport', 'Sibiu', 'Romania');
insert into airport(icao_code, name, city, country) values ('TMAN', 'TM Airport', 'Timisoara', 'Romania');
insert into airport(icao_code, name, city, country) values ('CJAN', 'CJ Airport', 'Cluj', 'Romania');
insert into airport(icao_code, name, city, country) values ('TKAN', 'TK Airport', 'Cluj', 'Turkey');
insert into airport(icao_code, name, city, country) values ('UKAN', 'UK Airport', 'London', 'UK');
insert into airport(icao_code, name, city, country) values ('ITAN', 'IT Airport', 'Roma', 'Italy');
insert into airport(icao_code, name, city, country) values ('SPAN', 'SP Airport', 'Madrid', 'Spain');
insert into airport(icao_code, name, city, country) values ('FRAN', 'FR Airport', 'Paris', 'France');
insert into airport(icao_code, name, city, country) values ('DEAN', 'DE Airport', 'Berlin', 'Germany');
insert into airport(icao_code, name, city, country) values ('GRAN', 'GR Airport', 'Athens', 'Greek');
Flight Table
insert into flight(flightnumber,departuretime,arrivaltime,distanceinkm,airportid_departure, airportid_arrival,numberofseats,airline_id) values
(1,to_date('2022/11/01 09:00:00', 'YYYY/MM/DD HH:MI:SS'), to_date('2022/11/01 12:00:00', 'YYYY/MM/DD HH:MI:SS'), 1200,'PJAN', 'GRAN',500,231);
insert into flight(flightnumber,departuretime,arrivaltime,distanceinkm,airportid_departure, airportid_arrival,numberofseats,airline_id) values
(2,to_date('2022/02/15 08:00:00', 'YYYY/MM/DD HH:MI:SS'), to_date('2022/02/16 12:00:00', 'YYYY/MM/DD HH:MI:SS'), 1200,'SBJN', 'GRAN',500,232);
insert into flight(flightnumber,departuretime,arrivaltime,distanceinkm,airportid_departure, airportid_arrival,numberofseats,airline_id) values
(3,to_date('2022/10/01 09:00:00', 'YYYY/MM/DD HH:MI:SS'), to_date('2022/10/02 12:00:00', 'YYYY/MM/DD HH:MI:SS'), 1200,'GRAN', 'CJAN',400,232);
insert into flight(flightnumber,departuretime,arrivaltime,distanceinkm,airportid_departure, airportid_arrival,numberofseats,airline_id) values
(4,to_date('2022/09/01 09:00:00', 'YYYY/MM/DD HH:MI:SS'), to_date('2022/09/02 12:00:00', 'YYYY/MM/DD HH:MI:SS'), 1200,'GRAN', 'TMAN',700,152);
insert into flight(flightnumber,departuretime,arrivaltime,distanceinkm,airportid_departure, airportid_arrival,numberofseats,airline_id) values
(5,to_date('2022/08/01 09:00:00', 'YYYY/MM/DD HH:MI:SS'), to_date('2022/08/02 12:00:00', 'YYYY/MM/DD HH:MI:SS'), 1200,'PJAN', 'GRAN',600,231);
--Paris-Berlin/Berlin-Paris
insert into flight(flightnumber,departuretime,arrivaltime,distanceinkm,airportid_departure, airportid_arrival,numberofseats,airline_id) values
(6,to_date('2022/05/06 09:00:00', 'YYYY/MM/DD HH:MI:SS'), to_date('2022/05/06 12:00:00', 'YYYY/MM/DD HH:MI:SS'), 800,'FRAN', 'DEAN',300,152);
insert into flight(flightnumber,departuretime,arrivaltime,distanceinkm,airportid_departure, airportid_arrival,numberofseats,airline_id) values
(7,to_date('2022/05/07 09:00:00', 'YYYY/MM/DD HH:MI:SS'), to_date('2022/05/07 12:00:00', 'YYYY/MM/DD HH:MI:SS'), 800,'DEAN', 'FRAN',300,152);
-- Spain-Turkey/Turkey-Spain
insert into flight(flightnumber,departuretime,arrivaltime,distanceinkm,airportid_departure, airportid_arrival,numberofseats,airline_id) values
(8,to_date('2022/05/07 09:00:00', 'YYYY/MM/DD HH:MI:SS'), to_date('2022/05/09 12:00:00', 'YYYY/MM/DD HH:MI:SS'), 4000,'SPAN', 'TKAN',800,112);
insert into flight(flightnumber,departuretime,arrivaltime,distanceinkm,airportid_departure, airportid_arrival,numberofseats,airline_id) values
(9,to_date('2022/05/08 09:00:00', 'YYYY/MM/DD HH:MI:SS'), to_date('2022/05/10 12:00:00', 'YYYY/MM/DD HH:MI:SS'), 4000,'SPAN', 'TKAN',800,112);
insert into flight(flightnumber,departuretime,arrivaltime,distanceinkm,airportid_departure, airportid_arrival,numberofseats,airline_id) values
(10,to_date('2022/05/17 09:00:00', 'YYYY/MM/DD HH:MI:SS'), to_date('2022/05/19 12:00:00', 'YYYY/MM/DD HH:MI:SS'), 4000,'TKAN', 'SPAN',800,112);
insert into flight(flightnumber,departuretime,arrivaltime,distanceinkm,airportid_departure, airportid_arrival,numberofseats,airline_id) values
(11,to_date('2022/05/18 09:00:00', 'YYYY/MM/DD HH:MI:SS'), to_date('2022/05/20 12:00:00', 'YYYY/MM/DD HH:MI:SS'), 4000,'TKAN', 'SPAN',800,112);
Ticket table
insert into ticket(ticketnumber, price, confirmationcode, flightnumber, passengerid) values
(1,200,'1232sde2asmd',7,37);
insert into ticket(ticketnumber, price, confirmationcode, flightnumber, passengerid) values
(2,210,'1232sde2asnd',7,36);
insert into ticket(ticketnumber, price, confirmationcode, flightnumber, passengerid) values
(3,250,'1232sde2astd',7,35);
insert into ticket(ticketnumber, price, confirmationcode, flightnumber, passengerid) values
(4,270,'1232sde2as2d',7,34);
insert into ticket(ticketnumber, price, confirmationcode, flightnumber, passengerid) values
(5,280,'1232sde2as0d',4,33);
insert into ticket(ticketnumber, price, confirmationcode, flightnumber, passengerid) values
(6,300,'1232sde2as9d',4,32);
insert into ticket(ticketnumber, price, confirmationcode, flightnumber, passengerid) values
(7,100,'1232sdv2as3d',4,31);
insert into ticket(ticketnumber, price, confirmationcode, flightnumber, passengerid) values
(8,200,'1232vde2as4d',1,30);
insert into ticket(ticketnumber, price, confirmationcode, flightnumber, passengerid) values
(9,120,'1252sde2as3d',2,29);
insert into ticket(ticketnumber, price, confirmationcode, flightnumber, passengerid) values
(10,290,'1232sde2ax3d',3,28);
insert into ticket(ticketnumber, price, confirmationcode, flightnumber, passengerid) values
(11,50,'1232sdy2as3d',3,27);
insert into ticket(ticketnumber, price, confirmationcode, flightnumber, passengerid) values
(12,90,'1232sae2as3d',6,26);
In the Ticket table I inserted 4 flights to France, 3 flights to Greece and another 2 to Greece (my mistake). It should be another Country.
So my query gives me:
FlightNumber NumberOfFlights
7 4
4 3
3 2
I want a result like that:
FlightNumber Country NumberOfFlights
7 France 4
4 Greece 3
3 Greece(mistake,some random country) 2
You need to join the tables together, then group by the country.
On Oracle 12c you can even use the newer offset syntax, and this will work in SQL Server also.
select
a.country,
count(*) as NumberOfTickets
from ticket as t
join flight as f on f.flightnumber = t.flightnumber
join airport as a on a.icao_code = f.airportid_arrival
group by
a.country
order by
NumberOfFlights desc
offset 0 rows fetch next 3 rows only;
If you wanted to get the top three flights, you could add flightnumber to the group by
select
t.flightnumber,
a.country,
count(*) as NumberOfTickets
from ticket as t
join flight as f on f.flightnumber = t.flightnumber
join airport as a on a.icao_code = f.airportid_arrival
group by
t.flightnumber,
a.country
order by
NumberOfFlights desc
offset 0 rows fetch next 3 rows only;
db<>fiddle
Maybe using oracle rank function. Something like this for oracle 11g:
select *
from (select country, flightCnt, dense_rank () over( order by flightCnt) as rnk
from (select a.country, count(f.flightNumber) as flightCnt
from ticket t, flight f, airport a
where t.flightNumber = f.flightNumber
and f.airportId_arrival = a.icao_code
group by a.country)
)
where rnk < 4
;

SQL - ORA-00911 - Invalid Character while trying to insert records

Oracle Live SQL.
I am trying to insert records for a table. The table goes through just fine but in each line of records, I am apparently entering some invalid characters. Of course, this is an extremely vague error. I don't know what's wrong.
I've tried changing the date values, tried removing values individually, but no matter what I get the same error.
CREATE TABLE Employee_Information (
employee varchar2(25) NOT NULL,
Address varchar2(50) NOT NULL,
Phone_number CHAR(10) NOT NULL,
Hire_date date,
Position varchar2(20),
Sales_Numbers varchar2(4),
Salary varchar2(6),
PRIMARY KEY(employee)
);
INSERT INTO Employee_Information (employee, address, phone_number,
hire_date, position, sales_numbers, salary)
VALUES (‘John Smith’, ‘123 1st street’, 5554622919, '2017-11-25', ‘assistant manager’, null, 55000);
INSERT INTO Employee_Information (employee, address, phone_number, hire_date, position, sales_numbers, salary)
VALUES (‘Bob Goldman’, ‘321 2nd street’, 5553392454, '2018-12-13', ‘cashier’, 345, 38000);
INSERT INTO Employee_Information (employee, address, phone_number, hire_date, position, sales_numbers, salary)
VALUES (‘Harry Wilson’, ‘222 3rd street’, 5553457777, '2018-01-10', ‘cashier’, 401, 42000);
INSERT INTO Employee_Information (employee, address, phone_number, hire_date, position, sales_numbers, salary)
VALUES (‘Sarah Adams’, ‘333 4th street’, 5555677654, '2019-03-15', ‘cashier’, 316, 36000);
INSERT INTO Employee_Information (employee, address, phone_number, hire_date, position, sales_numbers, salary)
VALUES (‘Alison Johnson’, ‘777 5th street’, 5559091111, '2017-11-01', ‘manager’, null, 60000);
ORA-00911 for every line of insertion.
try this: (date column you need to convert it to date and use ' chars instead of ‘ and ’)
INSERT INTO Employee_Information (employee, address, phone_number,
hire_date, position, sales_numbers, salary)
VALUES ('John Smith', '123 1st street', 5554622919, to_date('2017-11-25','yyyy-mm-dd') , 'assistant manager', null, 55000);
INSERT INTO Employee_Information (employee, address, phone_number, hire_date, position, sales_numbers, salary)
VALUES ('Bob Goldman', '321 2nd street', 5553392454, to_date('2018-12-13','yyyy-mm-dd'), 'cashier', 345, 38000);
INSERT INTO Employee_Information (employee, address, phone_number, hire_date, position, sales_numbers, salary)
VALUES ('Harry Wilson', '222 3rd street', 5553457777, to_date('2018-01-10','yyyy-mm-dd'), 'cashier', 401, 42000);
INSERT INTO Employee_Information (employee, address, phone_number, hire_date, position, sales_numbers, salary)
VALUES ('Sarah Adams', '333 4th street', 5555677654, to_date('2019-03-15','yyyy-mm-dd'), 'cashier', 316, 36000);
INSERT INTO Employee_Information (employee, address, phone_number, hire_date, position, sales_numbers, salary)
VALUES ('Alison Johnson', '777 5th street', 5559091111, to_date('2017-11-01','yyyy-mm-dd'), 'manager', null, 60000);

Converting time stamp to to_date in pandas

I am building and app for sql query genreation , i am able to generate it but receiving output in timestamp format , please help in converting it to to_date format.
Here is my code:-
from pandas import *
table_name="ADI"
file_name=pandas.read_excel('supermarke.xlsx')
def SQL_Insert(SOURCE, TARGET):
sql_texts = []
for index, row in SOURCE.iterrows():
sql_texts.append(
'INSERT INTO ' + TARGET + ' (' + str(', '.join(SOURCE.columns)) + ') VALUES ' + str(tuple(row.values))+";")
return ('\n'.join(sql_texts))
print(SQL_Insert(file_name, table_name))
Here is the output i am getting :-
INSERT INTO ADI (ID, Address, City, State, Country, Supermarket Name, Number of Employees, DATE) VALUES (1, '3666 21st St', 'San Francisco', 'CA 94114', 'USA', 'Madeira', 8, Timestamp('2018-01-12 00:00:00'));
INSERT INTO ADI (ID, Address, City, State, Country, Supermarket Name, Number of Employees, DATE) VALUES (2, '735 Dolores St', 'San Francisco', 'CA 94119', 'USA', 'Bready Shop', 15, Timestamp('2018-01-12 00:00:00'));
INSERT INTO ADI (ID, Address, City, State, Country, Supermarket Name, Number of Employees, DATE) VALUES (3, '332 Hill St', 'San Francisco', 'California 94114', 'USA', 'Super River', 25, Timestamp('2018-01-12 00:00:00'));
INSERT INTO ADI (ID, Address, City, State, Country, Supermarket Name, Number of Employees, DATE) VALUES (4, '3995 23rd St', 'San Francisco', 'CA 94114', 'USA', "Ben's Shop", 10, Timestamp('2018-01-12 00:00:00'));
Here what i am expecting :-
INSERT INTO ADI (ID, Address, City, State, Country, Supermarket Name, Number of Employees, DATE) VALUES (1, '3666 21st St', 'San Francisco', 'CA 94114', 'USA', 'Madeira', 8, TO_DATE('12/01/2018 00:00:00', 'MM/DD/YYYY HH24:MI:SS');
INSERT INTO ADI (ID, Address, City, State, Country, Supermarket Name, Number of Employees, DATE) VALUES (2, '735 Dolores St', 'San Francisco', 'CA 94119', 'USA', 'Bready Shop', 15, TO_DATE('12/01/2018 00:00:00', 'MM/DD/YYYY HH24:MI:SS');
INSERT INTO ADI (ID, Address, City, State, Country, Supermarket Name, Number of Employees, DATE) VALUES (3, '332 Hill St', 'San Francisco', 'California 94114', 'USA', 'Super River', 25,TO_DATE('12/01/2018 00:00:00', 'MM/DD/YYYY HH24:MI:SS');
INSERT INTO ADI (ID, Address, City, State, Country, Supermarket Name, Number of Employees, DATE) VALUES (4, '3995 23rd St', 'San Francisco', 'CA 94114', 'USA', "Ben's Shop", 10, TO_DATE('12/01/2018 00:00:00', 'MM/DD/YYYY HH24:MI:SS');
Please help me out here
If you must manually stitch together SQL query strings, try this (untested since I don't have your data):
import pandas as pd
table_name = 'ADI'
df = pandas.read_excel('supermarke.xlsx')
def SQL_Insert(SOURCE, TARGET):
sql_texts = []
for index, row in SOURCE.iterrows():
formatted_date = pd.to_datetime(row[-1]).strftime('%m/%d/%Y %H:%M:%S')
date_str = 'TO_DATE(\'{}\', \'MM/DD/YYYY HH24:MI:SS\')'.format(formatted_date)
sql_texts.append((
'INSERT INTO ' +
TARGET +
' (' +
str(', '.join(SOURCE.columns)) + ') VALUES ' +
str(tuple(row.values[:-1])) +
date_str +
';'))
return ('\n'.join(sql_texts))
print(SQL_Insert(df, table_name))
I recommend not manually building query strings. It's a solved problem. Further reading: How do I get a raw, compiled SQL query from a SQLAlchemy expression?

SQL loop inserts

I have a company table with list of companies name and company id.
Now there is a Value table which hold information about the company with reference to company id.
I need to first get the list and size of the companies and for all the companies insert a particular feature information in the Value table.
This means I need to have all companies having those features in the Value table.
I tried to use the below SQL which gives a compilation error. But the for loop works well without the insert.
DECLARE
x NUMBER(2) ;
BEGIN
FOR x IN (select distinct company_num from company where comp_IN_comp='T') LOOP
INSERT INTO VALUE (PROPERTY_NUM, DATA_GROUP, NUM_UPDATES,
CREATED_DATE, CREATED_BY, LAST_UPDATED_DATE, LAST_UPDATED_BY, VALUE) VALUES
('78', x ,'0', TO_DATE('2015-12-17 00:00:00', 'YYYY-MM-DD HH24:MI:SS'),
'ADMIN', TO_DATE('2015-12-17 00:00:00', 'YYYY-MM-DD HH24:MI:SS'), 'ADMIN', 'N');
END LOOP;
END;
You don't need a loop for this - just use an insert-select statement:
INSERT INTO VALUE (PROPERTY_NUM,
DATA_GROUP,
NUM_UPDATES,
CREATED_DATE,
CREATED_BY,
LAST_UPDATED_DATE,
LAST_UPDATED_BY,
VALUE)
SELECT DISTINCT '78',
company_num,
'0',
TO_DATE('2015-12-17 00:00:00', 'YYYY-MM-DD HH24:MI:SS'),
'ADMIN',
TO_DATE('2015-12-17 00:00:00', 'YYYY-MM-DD HH24:MI:SS'),
'ADMIN',
'N'
FROM company
WHERE comp_in_comp='T'

Insertion in database of type timestamp and data containing apostrophe using ORACLE-11g

I have creater the table successfully as follows:
CREATE TABLE TOY_STORE
(
TOY_STORE_ID NUMBER(3) PRIMARY KEY,
TOY_STORE_NAME VARCHAR2(30) NOT NULL,
CITY VARCHAR2(30) DEFAULT 'Delhi',
PHONENUMBER NUMBER(10) NOT NULL UNIQUE,
STORE_OPENING_TIME TIMESTAMP,
STORE_CLOSING_TIME TIMESTAMP
);
ALTER TABLE TOY_STORE ADD CHECK (EXTRACT(HOUR FROM CAST (TO_CHAR (STORE_OPENING_TIME, 'YYYY-MON-DD HH24:MI:SS') AS TIMESTAMP)) > 8 || NULL);
ALTER TABLE TOY_STORE ADD CHECK (EXTRACT(HOUR FROM CAST(TO_CHAR(STORE_CLOSING_TIME, 'YYYY-MON-DD HH24:MI:SS') AS TIMESTAMP)) < 22 || NULL);
Now I want to enter data in the table. I executed the following command (here the second data is "Kid's Cave"),
INSERT INTO TOY_STORE VALUES(1, 'Kid''s Cave', 'Delhi', 9912312312, 2014-04-01 09:10:12, 2014-04-01 21:42:05);
But it showed the following error..
ORA-00917: missing comma
Please explain
You need to put the dates inside ''. Try this:
INSERT INTO TOY_STORE
VALUES(1, 'Kid''s Cave', 'Delhi', 9912312312, '2014-04-01 09:10:12', '2014-04-01 21:42:05');
On a side note:
I will suggest you to use varchar() to store PhoneNumbers instead of Number datatype
Dates don't have a single quote in your query - Use like so:
'2014-04-01 09:10:12', '2014-04-01 21:42:05'
The problem isn't the ', it's the fact that you are not quoting the date literals. Oracle interprets it as several integer literals with operators between them, and fails because there's no comma separating them.
Surrounding them by quotes (') should work:
INSERT INTO TOY_STORE VALUES
(1,
'Kid''s Cave',
'Delhi',
9912312312,
'2014-04-01 09:10:12',
'2014-04-01 21:42:05');
But it's a bad practice, as it assumes the format matches the database's default date format, which makes your code error prone and unprortable. A better approach would be to explicitly convert these values to timestamps with an explicitly stated format:
INSERT INTO TOY_STORE VALUES
(1,
'Kid''s Cave',
'Delhi',
9912312312,
TO_TIMESTAMP('2014-04-01 09:10:12', 'YYYY-MM-DD HH24:MI:SS'),
TO_TIMESTAMP('2014-04-01 21:42:05', 'YYYY-MM-DD HH24:MI:SS'));
Try using to_date to convert string into date
INSERT INTO TOY_STORE
VALUES (1, 'Kid''s Cave', 'Delhi', 9912312312,
to_date('2014-04-01 09:10:12', 'yyyy-mm-dd hh24:mi:ss'),
to_date('2014-04-01 21:42:05', 'yyyy-mm-dd hh24:mi:ss'));
you can also use TIMESTAMP literal
INSERT INTO TOY_STORE
VALUES (1, 'Kid''s Cave', 'Delhi', 9912312312,
TIMESTAMP '2014-04-01 09:10:12',
TIMESTAMP '2014-04-01 21:42:05');