Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Select date in oracle db

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

>Solution :

  • 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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading