Insert SQL - MM variable? - sql

do i need to change the data type of the DATE column to VARCHAR?
SQL> INSERT INTO BW_CLASS VALUES(`PC101', `MS OFFICE BASICS', `INDIANA JONES','18','1000',
2 TO_DATE('01-10-2013 10:30 AM', 'MM-DD-YYYY HH:Mm PM'),
3 TO_DATE('05-10-2013 10:30 AM', 'MM-DD-YYYY HH:Mm PM'),
4 `1276';
SP2-0552: Bind variable "MM" not declared.
SQL> desc bw_class
Name Null? Type
CLASS_ID NOT NULL CHAR(5)
CLASS_NAME NOT NULL VARCHAR2(40)
PROFESSOR NOT NULL VARCHAR2(50)
NUMBER_OF_STUDENTS NUMBER(6,2)
COST NUMBER(6,2)
START_DATE NOT NULL DATE
END_DATE NOT NULL DATE
ROOM_NUM VARCHAR2(3)

please use this query
INSERT INTO BW_CLASS VALUES('PC101', 'MS OFFICE BASICS', 'INDIANA JONES','18','1000',
TO_DATE('01-10-2013 10:30 AM', 'MM-DD-YYYY HH:MI PM'),
TO_DATE('05-10-2013 10:30 AM', 'MM-DD-YYYY HH:MI PM'),
'1276';

You used a wrong quote. All parameters need to be quoted using ' in both sides, but you used ` in some places. So you should try this:
INSERT INTO BW_CLASS VALUES('PC101', 'MS OFFICE BASICS', 'INDIANA JONES','18','1000',
TO_DATE('01-10-2013 10:30 AM', 'MM-DD-YYYY HH:MI PM'),
TO_DATE('05-10-2013 10:30 AM', 'MM-DD-YYYY HH:MI PM'),
'1276';
And you don't need to write 2 3 4 at the beginning of lines, why did you do that?

Related

In Oracle SQL developer how do you insert date and time

I am Unable to insert date data and time data into oracle table. Please find the below query and error
INSERT INTO Sunday_Service (
Service_ID,
Service_date,
Start_time,
End_time,
Service_location,
No_of_children)
Values (
Seq_service_id.nextVal,
TO_DATE('YYYY-MM-DD', '2022-07-03'),
TO_DATE('hh24:mi:ss', '09:00:00'),
TO_DATE('hh24:mi:ss', '10:15:00'),
'RM 2101',
'10');
error:
Error report ORA-01821
You have your parameters switched in your TO_DATE function calls. Your TO_DATE function calls should look like this:
TO_DATE ('2022-07-03', 'YYYY-MM-DD'),
TO_DATE ('09:00:00', 'hh24:mi:ss'),
TO_DATE ('10:15:00', 'hh24:mi:ss')
You have the arguments to to_date() the wrong way around:
INSERT INTO Sunday_Service (Service_ID, Service_date,
Start_time, End_time,
Service_location, No_of_children)
VALUES (Seq_service_id.nextVal,
TO_DATE('2022-07-03', 'YYYY-MM-DD'),
TO_DATE('09:00:00', 'hh24:mi:ss'),
TO_DATE('10:15:00', 'hh24:mi:ss'),
'RM 2101',
'10');
But you probably want to combine the time and date, rather than holding them in separate columns; so if you removed service_date from your table you could do:
INSERT INTO Sunday_Service (Service_ID,
Start_time, End_time,
Service_location, No_of_children)
VALUES (Seq_service_id.nextVal,
TO_DATE('2022-07-03 09:00:00', 'YYYY-MM-DD HH24:MI:SS'),
TO_DATE('2022-07-03 10:15:00', 'YYYY-MM-DD HH24:MI:SS'),
'RM 2101',
'10');
Apart from anything else, that will make it possible to handle service calls that span midnight or multiple days.
You could also use timestamp literals:
...
VALUES (Seq_service_id.nextVal,
TIMESTAMP '2022-07-03 09:00:00',
TIMESTAMP '2022-07-03 10:15:00',
...
or slightly more explcitly:
...
VALUES (Seq_service_id.nextVal,
CAST(TIMESTAMP '2022-07-03 09:00:00' AS DATE),
CAST(TIMESTAMP '2022-07-03 10:15:00' AS DATE),
...
If no_of_children is a number column, as it appears, then the last value should be a number - 10 rather than '10'.

sql query to pl sql procedure

this is my requirement .i want fetch the record from one table and store it in another temporary table.i wrote as query.but dont know how to make it as procedure by declaring varibales and so.
Daily new customers data will gets inserted in table.I only want to fetch the customer data who signed attribute_value as 'TOY_GIFT' from last 10 to till today's date. i want to run this as procedure for every 10 days.
CREATE
OR
INSERT INTO
cst_cust_attributes_tmp (ORGANIZATION_ID, CUST_ID, ATTRIBUTE_ID, ATTRIBUTE_SEQ, ATTRIBUTE_VALUE, ACTIVE_FLAG, CREATE_DATE, CREATE_USER, UPDATE_DATE, UPDATE_USER)
SELECT
ORGANIZATION_ID,
CUST_ID,
ATTRIBUTE_ID,
ATTRIBUTE_SEQ,
ATTRIBUTE_VALUE,
ACTIVE_FLAG,
CREATE_DATE,
CREATE_USER,
UPDATE_DATE,
UPDATE_USER
FROM
cst_cust_attributes
WHERE
create_date between to_date(to_char(sysdate - 10, 'DD-MON-YYYY HH:MI:SS AM'), 'DD-MON-YYYY HH:MI:SS AM') and to_date(to_char(sysdate, 'DD-MON-YYYY HH:MI:SS AM'), 'DD-MON-YYYY HH:MI:SS AM')
and attribute_value = 'TOY_GIFT' ;
//
Thanks in advance..
You need to create a proc to insert records, and set up a dbms job to execute it every 10 days.
Like, procedure :
create or replace procedure LOAD_CUSTOMERS is
BEGIN
INSERT INTO
cst_cust_attributes_tmp (ORGANIZATION_ID, CUST_ID, ATTRIBUTE_ID, ATTRIBUTE_SEQ, ATTRIBUTE_VALUE, ACTIVE_FLAG, CREATE_DATE, CREATE_USER, UPDATE_DATE, UPDATE_USER)
SELECT
ORGANIZATION_ID,
CUST_ID,
ATTRIBUTE_ID,
ATTRIBUTE_SEQ,
ATTRIBUTE_VALUE,
ACTIVE_FLAG,
CREATE_DATE,
CREATE_USER,
UPDATE_DATE,
UPDATE_USER
FROM
cst_cust_attributes
WHERE
create_date between to_date(to_char(sysdate - 10, 'DD-MON-YYYY HH:MI:SS AM'), 'DD-MON-YYYY HH:MI:SS AM') and to_date(to_char(sysdate, 'DD-MON-YYYY HH:MI:SS AM'), 'DD-MON-YYYY HH:MI:SS AM')
and attribute_value = 'TOY_GIFT' ;
COMMIT;
END;
DBMS Job:
begin
sys.dbms_scheduler.create_job(job_name => 'LOAD_CUSTOMERS_JOB',
job_type => 'STORED_PROCEDURE',
job_action => 'LOAD_CUSTOMERS', -- YOUR PROC NAME
start_date => to_date('05-12-2019 00:00:00', 'dd-mm-yyyy hh24:mi:ss'),
repeat_interval => 'Freq=Daily;Interval=10',
end_date => to_date(null),
job_class => 'DEFAULT_JOB_CLASS',
enabled => true,
auto_drop => false,
comments => '');
end;

Problems inserting datetime data into a table

I'm trying to insert data into this table, but it doesn't work, and I was changing the format to DDMMYY, DD/MM/YY, YYYY-MM-DD... and nothing change the error.
Code:
GO
create table PeriodoAcademico(
CodPeriodoAcad varchar(50) not null primary key,
Descripcion varchar(50) not null,
FechaInicio datetime not null,
FechaFin datetime not null,
FechaInicioClases datetime not null,
FechaFinClases datetime not null,
FechaLimitePago datetime not null,
FechaLimitePrematricula datetime not null,
FechaLimiteRetiro datetime not null,
FechaLimitePublicacion datetime not null
);
GO
begin tran
insert into PeriodoAcademico
values
('2019-2020-2', 'Enero-Abril 2020', '2020-01-04 06:00:00 AM', '2020-04-12 11:59:99 PM', '2020-01-08 06:00:00 AM', '2020-04-08 11:59:99 PM',
'2020-04-08 11:59:99 PM', '2020-03-08 11:59:99 PM', '2020-03-21 11:59:99 PM', '2020-04-10 11:59:99 PM');
commit
GO
Error:
Msg 242, Level 16, State 3, Line 77
The conversion of a varchar data type to a datetime data type resulted in an out-of-range value.
The statement has been terminated.
Completion time: 2020-03-30T17:01:44.3689635-04:00
There are only 60 seconds in a minute.
So this '2020-04-08 11:59:99 PM' is invalid. This '2020-04-08 11:59:59 PM' is fine. eg
select cast('2020-03-08 11:59:59 PM' as datetime)
outputs
2020-03-08 23:59:59.000

Insert Statement error, ORA-00984: column not allowed here

any help guys I got error column not allowed here for datetime !
INSERT INTO MEMBERS_CONTRIBUTIONS (
CONTRIBUTION_TYPE,
FROM_DATE,
TO_DATE,
ADDED_PERIOD_IN_MONTHS,
MEMBER_AMOUNT,
THE_CURRENCY,
MATURITY_DATE
) VALUES (
4,
convert(datetime, '6/1/2016 12:00:00 AM', 5),
convert(datetime, '6/1/2016 12:00:00 AM', 5),
0,
2500,
'OMR',
convert(datetime, '6/30/2016 12:00:00 AM', 5)
);
You are trying to use the SQL Server CONVERT() function in Oracle - the Oracle CONVERT() function converts from one character-set to another and does not do what you want.
Instead, you can use a date literal:
INSERT INTO MEMBERS_CONTRIBUTIONS (
CONTRIBUTION_TYPE,
FROM_DATE,
TO_DATE,
ADDED_PERIOD_IN_MONTHS,
MEMBER_AMOUNT,
THE_CURRENCY,
MATURITY_DATE
) VALUES (
4,
DATE '2016-06-01',
DATE '2016-06-01',
0,
2500,
'OMR',
DATE '2016-06-30'
);
In Oracle, all DATE types have both a date and time component - the date literal syntax will just set the time component to 00:00:00 (or 12:00:00 AM in a 12 hour clock).
Or if you want to specify the time component then you can use the timestamp literal (which Oracle will implicitly cast to a DATE type if that is the type of the column you are storing it in):
INSERT INTO MEMBERS_CONTRIBUTIONS (
CONTRIBUTION_TYPE,
FROM_DATE,
TO_DATE,
ADDED_PERIOD_IN_MONTHS,
MEMBER_AMOUNT,
THE_CURRENCY,
MATURITY_DATE
) VALUES (
4,
TIMESTAMP '2016-06-01 00:00:00',
TIMESTAMP '2016-06-01 00:00:00',
0,
2500,
'OMR',
TIMESTAMP '2016-06-30 00:00:00'
);
Or you could explicitly cast a string literal to a date using the TO_DATE() function:
INSERT INTO MEMBERS_CONTRIBUTIONS (
CONTRIBUTION_TYPE,
FROM_DATE,
TO_DATE,
ADDED_PERIOD_IN_MONTHS,
MEMBER_AMOUNT,
THE_CURRENCY,
MATURITY_DATE
) VALUES (
4,
TO_DATE( '6/1/2016 12:00:00 AM', 'MM/DD/YYYY HH12:MI:SS AM' ),
TO_DATE( '6/1/2016 12:00:00 AM', 'MM/DD/YYYY HH12:MI:SS AM' ),
0,
2500,
'OMR',
TO_DATE( '6/30/2016 12:00:00 AM', 'MM/DD/YYYY HH12:MI:SS AM' )
);
The expression convert(datetime, '6/1/2016 12:00:00 AM', 5) calls for a column with the name datetime. But your insert statement doesn't offer a context involving any columns at all, so the query parser can't make any sense of datetime. Hence your ORA-00948 error.
I guess you're trying to put a date/time constant, which from your example could mean either 1-Jun-2016 or 6-Jan-2016, into a date datatype. You need to use the TO_DATE() function to convert your strings to that format. I'm not going to suggest a particular form of the call, because I don't know exactly how your strings are formatted.

BITAND SQL - Operation between hexadecimal data Oracle

I need resolve the following situation:
I have the following twelve rows in a table A_5MIN_TST1 (the data to be compared are hexa, but examples works with decimal values):
UTCTIME|TLQ_INST
01/08/2013 01:05:00 a.m.|32
01/08/2013 01:10:00 a.m.|128
01/08/2013 01:15:00 a.m.|8
01/08/2013 01:20:00 a.m.|32
01/08/2013 01:25:00 a.m.|1
01/08/2013 01:30:00 a.m.|10
01/08/2013 01:35:00 a.m.|100
01/08/2013 01:40:00 a.m.|1000
01/08/2013 01:45:00 a.m.|2000
01/08/2013 01:50:00 a.m.|3000
01/08/2013 01:55:00 a.m.|4000
Doing a select I must analyze each bit of the tlq_inst column (hexadecimal data) and decide:
If some value of tlq_inst is
= 8
or
= 32
or
= 128
then write = 8.
When tlq_inst doesn't is 8, 32, 128 then write the first value of tlq_inst, over the range.
I have tried with this query:
SELECT DECODE(POWER(2,BITAND(tlq_inst, 168)), 1, 'OK','Q') salida
FROM A_5MIN_TST1
WHERE utctime >= TO_DATE ('01/08/2013 01:00:01','dd/mm/yyyy hh24:mi:ss')
AND utctime < TO_DATE ('01/08/2013 02:00:00','dd/mm/yyyy hh24:mi:ss')
AND POINTNUMBER = 330062;
And I received these results:
SALIDA
Q
Q
Q
Q
OK
Q
Q
Q
Q
Q
Q
Q
Resuming, on these 12 values, I need to do:
Get 'Q' if the comparison condition with mask is met.
Get the first value of tlq_inst, when the comparison with the mask, is NOT true.
If possible, do the same but inside where
With this query I managed to get 12 values, but I need to get only one.
Could you help me to resolve this problem?
CREATE TABLE A_5MIN_TST1
(
UTCTIME DATE NOT NULL,
POINTNUMBER INTEGER NOT NULL,
SITEID INTEGER,
VALOR_INST FLOAT(126),
TLQ_INST INTEGER,
VALOR_PROM FLOAT(126),
TLQ_PROM INTEGER,
VALOR_MAX FLOAT(126),
TLQ_MAX INTEGER,
UTCTIME_MAX DATE,
VALOR_MIN FLOAT(126),
TLQ_MIN INTEGER,
UTCTIME_MIN DATE
)
TABLESPACE USERS
PCTUSED 0
PCTFREE 10
INITRANS 1
MAXTRANS 255
STORAGE (
INITIAL 64K
MINEXTENTS 1
MAXEXTENTS UNLIMITED
PCTINCREASE 0
BUFFER_POOL DEFAULT
)
LOGGING
NOCOMPRESS
NOCACHE
NOPARALLEL
MONITORING;
ALTER TABLE A_5MIN_TST1 ADD (
PRIMARY KEY
(UTCTIME, POINTNUMBER)
USING INDEX
TABLESPACE USERS
PCTFREE 10
INITRANS 2
MAXTRANS 255
STORAGE (
INITIAL 64K
MINEXTENTS 1
MAXEXTENTS UNLIMITED
PCTINCREASE 0
));
SET DEFINE OFF;
Insert into A_5MIN_TST1
(UTCTIME, TLQ_INST)
Values
(TO_DATE('08/01/2013 01:05:00', 'MM/DD/YYYY HH24:MI:SS'), 32);
Insert into A_5MIN_TST1
(UTCTIME, TLQ_INST)
Values
(TO_DATE('08/01/2013 01:10:00', 'MM/DD/YYYY HH24:MI:SS'), 128);
Insert into A_5MIN_TST1
(UTCTIME, TLQ_INST)
Values
(TO_DATE('08/01/2013 01:15:00', 'MM/DD/YYYY HH24:MI:SS'), 8);
Insert into A_5MIN_TST1
(UTCTIME, TLQ_INST)
Values
(TO_DATE('08/01/2013 01:20:00', 'MM/DD/YYYY HH24:MI:SS'), 32);
Insert into A_5MIN_TST1
(UTCTIME, TLQ_INST)
Values
(TO_DATE('08/01/2013 01:25:00', 'MM/DD/YYYY HH24:MI:SS'), 1);
Insert into A_5MIN_TST1
(UTCTIME, TLQ_INST)
Values
(TO_DATE('08/01/2013 01:30:00', 'MM/DD/YYYY HH24:MI:SS'), 10);
Insert into A_5MIN_TST1
(UTCTIME, TLQ_INST)
Values
(TO_DATE('08/01/2013 01:35:00', 'MM/DD/YYYY HH24:MI:SS'), 100);
Insert into A_5MIN_TST1
(UTCTIME, TLQ_INST)
Values
(TO_DATE('08/01/2013 01:40:00', 'MM/DD/YYYY HH24:MI:SS'), 1000);
Insert into A_5MIN_TST1
(UTCTIME, TLQ_INST)
Values
(TO_DATE('08/01/2013 01:45:00', 'MM/DD/YYYY HH24:MI:SS'), 2000);
Insert into A_5MIN_TST1
(UTCTIME, TLQ_INST)
Values
(TO_DATE('08/01/2013 01:50:00', 'MM/DD/YYYY HH24:MI:SS'), 3000);
Insert into A_5MIN_TST1
(UTCTIME, TLQ_INST)
Values
(TO_DATE('08/01/2013 01:55:00', 'MM/DD/YYYY HH24:MI:SS'), 4000);
COMMIT;
Here is a statement giving you Q when at least one record matches the bitmask and the earliest TLQ_INST otherwise. It uses KEEP DENSE_RANK. It orders the records by utctime, gets the earliest record and returns the tlq_inst of that record. In case there are more records with the same earliest time, it returns the maximum tlq_inst of these records.
select
case when max(bitand(tlq_inst, 168)) = 0 then
max(tlq_inst) keep (dense_rank first order by utctime)
else
'Q'
end as result
from a_5min_tst1
where utctime >= to_date ('01/08/2013 01:00:01','dd/mm/yyyy hh24:mi:ss')
and utctime < to_date ('01/08/2013 02:00:00','dd/mm/yyyy hh24:mi:ss')
and pointnumber = 330062;