Select date in oracle db - sql

I set or insert time in a postgres table value with now()::timestamp
But it does not work in oracle db.
This is the table:
create table types
(
id number not null,
description varchar(255) not null,
created_at timestamp null,
updated_at timestamp null,
deleted_at timestamp null,
CONSTRAINT autenticacion_id PRIMARY KEY (id)
);
So to insert the data I make:
insert into types (id, description) values (1, 'hello world', now()::timestamp)
But I get:
ORA-00917: missing comma
insert into types (id, description) values (1, 'hello world', (select sysdate from dual))
I get:
ORA-00942: table or view does not exist

You are providing 3 values and have only given 2 columns that you want to insert into.
SYSDATE is a DATE data type and SYSTIMESTAMP is a TIMESTAMP data type; they both have a date and time component but SYSTIMESTAMP also has fractional seconds (and a time zone, but that will be ignored as the column you are inserting into is a TIMESTAMP and not a TIMESTAMP WITH TIME ZONE data type).
What you probably want is:
INSERT INTO types (id, description, created_at)
VALUES (1, 'hello world', SYSTIMESTAMP)
However, you may want to consider that the timestamp should be in a specific time zone (typically UTC):
INSERT INTO types (id, description, created_at)
VALUES (1, 'hello world', SYSTIMESTAMP AT TIME ZONE 'UTC')
Note: You can wrap a value in (SELECT value FROM DUAL) but it is not necessary.
fiddle

Related

How to insert a date value stored in another table and increment it by x days?

I'm using a PostgreSQL database and have one table "event_date" with "started_at" (DATE format, e.g. '2019-10-29') values which are also the PRIMARY KEY in this table.
Now I want to insert this "started_at" value in another table "event_days" but increasing the date by x days.
Can someone explain how to achieve this? Thanks a lot!
Use an INSERT with a SELECT:
insert into other_table (started_at, location, moderator)
select started_at + 42, 'Hall 5', 'Tom'
from event_days;
+ 42 add 42 days to the date value.
if you really need insert the related record you could use a insert select
insert into event_days(started_at, location , moderator)
select started_at + interval '10 day', 'hall 5' , 'Tom'
from event_date

Insert statement Oracle

Here is my table
create table reservations (
ResID int NOT NULL,
GuestID int,
HotelID int,
Check_in DATE,
Check_out DATE,
RoomType varchar2(15),
Price Dec(8,2),
PRIMARY KEY (ResID),
CONSTRAINT FK_GuestIDX FOREIGN KEY(GuestID) REFERENCES Guests(GuestID),
CONSTRAINT FK_HotelID FOREIGN KEY(HotelID) REFERENCES Hotel(HotelID)
);
Table was created with no problems. Now Im trying to populate the table
Here is my insert statement
insert into reservations (1, 1, 2, '17-DEC-2018', '21-DEC-2018', 'Suite', 87.03);
and here is the error that I'm getting
ORA-00928: missing SELECT keyword
What could be the cause for this?
Or missing values:
insert into reservations
values (1, 1, 2, '17-DEC-2018', '21-DEC-2018', 'Suite', 87.03);
That said, I would recommend writing this as:
insert into reservations (ResID, GuestID, HotelID, Check_in, Check_out DATE, RoomType, Price)
values (1, 1, 2, DATE '2018-12-17', DATE '2018-12-21', 'Suite', 87.03);
Note:
List the columns after the insert. This can really prevent hard-to-debug errors.
This uses the DATE keyword to introduce a date constant.
You can also write this using SELECT:
insert into reservations (ResID, GuestID, HotelID, Check_in, Check_out DATE, RoomType, Price)
select 1, 1, 2, DATE '2018-12-17', DATE '2018-12-21', 'Suite', 87.03
from dual;

insert date difference in last column

I want to insert date difference of 4th and 5th column in the 6th column but it is giving me error saying column not allowed.
insert into leave25 vALUES (4, 4, 'SICK', TO_DATE('22-AUG-14','DD-MON-YY'), TO_DATE('22-SEP-14','DD-MON-YY'), startdate-enddate)\\
Always specify the column names explicitly in your INSERT statements. It is safer that way.
If you want to avoid writing the dates again, you could select the values from a subquery.
INSERT INTO leave25
(col1,
col2,
col3,
startdate,
enddate,
days)
SELECT t.*,
startdate - enddate
FROM (SELECT 4,
4,
'SICK',
to_date('22-AUG-14', 'DD-MON-YY') as startdate,
to_date('22-SEP-14', 'DD-MON-YY') as enddate
FROM dual) t;
Furthermore, I agree with Aleksej, it is better to have this difference stored as a virtual column rather than a column itself.
You can not do it this way, you need to esplicitly use the values you want to insert:
INSERT INTO leave25
VALUES (
4,
4,
'SICK',
TO_DATE('22-AUG-14', 'DD-MON-YY'),
TO_DATE('22-SEP-14', 'DD-MON-YY'),
TO_DATE('22-SEP-14', 'DD-MON-YY') - TO_DATE('22-AUG-14', 'DD-MON-YY')
Besides, if this column always contains that difference, are you sure you need to store this redundant information?
A different approach could be by using a trigger to populate that column without explicitly giving it a value, but this depends on your environment and your needs.
Use a virtual column:
CREATE TABLE Leave25(
EMPLOYEEID INTEGER
CONSTRAINT LEAVE25__EMPID__FK REFERENCES Employee24,
LEAVEID INTEGER
CONSTRAINT LEAVE25__LEAVEID__PK PRIMARY KEY,
LEAVETYPE VARCHAR2(20)
CONSTRAINT LEAVE25__LEAVETYPE__NN NOT NULL
CONSTRAINT LEAVE25__LEAVETYPE__CHK CHECK (
LEAVETYPE IN 'EARNED', 'SICK'
),
STARTDATE DATE
CONSTRAINT LEAVE25__STARTDATE__NN NOT NULL,
ENDDATE DATE
CONSTRAINT LEAVE25__ENDDATE__NN NOT NULL,
DURATION NUMBER
GENERATED ALWAYS AS ( ENDDATE - STARTDATE ) VIRTUAL
);
And create a sequence to manage the primary key:
CREATE SEQUENCE LEAVE25__LEAVEID__SEQ;
Then you can do:
INSERT INTO leave25 (
EMPLOYEEID, LEAVEID, LEAVETYPE, STARTDATE, ENDDATE
) VALUES (
4, LEAVE25__LEAVEID__SEQ.NEXTVAL, 'SICK', DATE '2014-08-22', DATE '2014-09-22'
)

Copy data from integer field to jsonb field

I have the following schema
CREATE TABLE survey_results (
id integer NOT NULL,
raw jsonb DEFAULT '{}'::jsonb,
survey_id integer NOT NULL,
created_at timestamp without time zone,
updated_at timestamp without time zone
);
INSERT INTO survey_results (id, survey_id, raw, created_at, updated_at)
VALUES (1, 10, '{"survey": {}}', '2018-01-10', '2018-01-11');
INSERT INTO survey_results (id, survey_id, raw, created_at, updated_at)
VALUES (2, 20, '{"survey": {}}', '2018-01-12', '2018-01-12');
I want to copy survey_id value to raw->survey->survey_id key. I've tried to do that in the following way but that didn't work:
UPDATE survey_results SET raw#>>'{survey, survey_id}' = survey_id;
Is there way of doing that in PostgreSQL?
http://sqlfiddle.com/#!17/ed50f/1
You need to use jsonb_set()
update survey_results
set raw = jsonb_set(raw, '{survey, survey_id}', to_jsonb(survey_id), true);
Online example: http://rextester.com/GTBTY13915

Time Stamp value not inserting in oracle

I have following table in Oracle:
DESC TIME_PERIOD
Name Null Type
---------- -------- ------------
TIME_ID NOT NULL NUMBER(2)
START_TIME NOT NULL TIMESTAMP(6)
END_TIME NOT NULL TIMESTAMP(6)
I am inserting values, but values are not inserted. I am using the following query.
INSERT INTO TIME_PERIOD (TIME_ID,START_TIME,END_TIME)
VALUES (1, TO_DSINTERVAL('0 23:59:59'), TO_DSINTERVAL('0 23:59:59'));
How can I insert value in Oracle?
I want time like this
10:00 Am
11:00Am
1:00pm
Not quite sure what you're trying to do, but to insert a TIMESTAMP you need to use the SYSTIMESTAMP() function:
e.g.
INSERT INTO TIME_PERIOD (START_TIME)
VALUES (SYSTIMESTAMP);