Insert with the current date and time - sql

INSERT INTO PUBLIC.PROJECT(ID, LIBELLE, DESCRIPTION, STATUT, DATE_CREATION, DATE_MODIFICATION, USER_CREATEUR_ID, REFERENT_ID, DATE_DEBUT, DATE_FIN, TYPE, STATUT_PROJET) VALUES
(2651, 'Projet 1', NULL, 'START', DATE '2019-11-15', DATE '2019-11-15', NULL, 952, TIMESTAMP '2019-11-14 23:00:00', TIMESTAMP '2019-12-14 23:00:00', NULL, NULL);
I want to modify this request and use current_timestamp to have the current date and the current date with time
Example: 2019-11-19 23:00:00
I try to do that : date (current_timestamp + time '23:00') but i have some errors.
Someone have idea ?

Use current_date instead:
current_date + time '23:00'
And you don't need to cast the result, use it as written.

Related

How can i insert DateTime in SQL?

how can i insert this DateTime in SQL?
1/21/2020 8:30:53.527000 AM +00:00
I know that for 12/12/2020 we can make TO_DATE('12/12/2020', 'DD/MM/YYYY')
In Oracle, in your INSERT statement you can use a TIMESTAMP literal:
TIMESTAMP '2020-01-21 08:30:53.527000 +00:00'
Or use TO_TIMESTAMP_TZ:
TO_TIMESTAMP_TZ(
'1/21/2020 8:30:53.527000 AM +00:00',
'MM/DD/YYYY HH12:MI:SS.FF6 AM TZH:TZM'
)

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:
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

How to select within a time range and not within a time range

I have employees. It is currently 9:30 am. I want to select everyone who is working right now. Then in a separate sql statement, I want to select everyone who is not working right now. How can I do this? I am having trouble wrapping my head around the logic. I have used the time field rather than datetime on purpose.
My table with sample data
CREATE TABLE shifts (
id int unsigned
AUTO_INCREMENT
PRIMARY KEY,
name varchar(64)
NOT NULL,
start_time time
NOT NULL,
end_time time
NOT NULL
);
INSERT INTO shifts (id, name, start_time, end_time) VALUES
(1, 'Bob', '09:00:00', '17:00:00'),
(2, 'Jack', '09:30:00', '14:00:00'),
(3, 'Bill', '15:00:00', '03:00:00'),
(4, 'James', '23:30:00', '10:00:00'),
(5, 'Sarah', '14:30:00', '21:00:00'),
(6, 'Marry', '03:00:00', '09:30:00');
People who are working:
name
Bob
Jack
James
People who are not working:
name
Bill
Sarah
Marry
I cannot wrap my head around it, my attempt at people who are working is wrong but...:
SELECT name
FROM shifts
WHERE start_time <= '09:30:00' AND
end_time >= '09:30:00';
You can select everyone who is working now using:
select s.*
from shifts s
where (start_time < end_time and '09:30:00' >= start_time and '09:30:00' < end_time) or
(start_time > end_time and ('09:30:00' >= start_time or '09:30:00' < end_time));
For employees who are not working, you use similar logic. The simplest method is:
select s.*
from shifts s
where not ((start_time < end_time and '09:30:00' >= start_time and '09:30:00' < end_time) or
(start_time > end_time and ('09:30:00' >= start_time or '09:30:00' < end_time))
);

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);

Get correct time zone in SQL 2012 on select statement

I need to get this SQL query to output the time in the correct time zone when running the following query.
SELECT
CAST([StartDateTime] AS time(0)) AS 'time'
from appointment
When I run this it currently return everything in UTC time and I need EST. I have tried the switchoffset command, and it works, but I only need time to display and not date and time.
This should work:
SELECT CAST(DATEADD(HH,-5,[StartDateTime]) as time) AS 'time'
FROM appointment
Try this
DECLARE #appointment TABLE
(
id integer,
StartDateTime datetime
)
INSERT INTO #appointment SELECT 1, '1/1/2014 13:30'
INSERT INTO #appointment SELECT 2, '1/4/2014 4:00'
INSERT INTO #appointment SELECT 3, '1/6/2014 15:30'
INSERT INTO #appointment SELECT 4, '1/8/2014 23:00'
SELECT
CAST(SWITCHOFFSET(CONVERT(DATETIMEOFFSET, StartDateTime), '+01:00') as TIME(0)) as StartDateTime
FROM #appointment