Home > Mobile >  SQL - Inster into with now() minus one day
SQL - Inster into with now() minus one day

Time:11-23

I want change this request. Actualy this request insert into always 2021-11-11 at 10:50:00

INSERT INTO my_table(name, start_date) VALUES('test', '2021-11-11 10:50:00);

I use a script.sql for my H2 database (continous delivery). I want change my request to Now() minus one day at 10:50:00.

CodePudding user response:

Subtract a day from the current date, then add the time.

INSERT INTO my_table (name, start_date) VALUES 
('test', DATEADD(DAY, -1, CURRENT_DATE)   TIME '10:50:00');

CodePudding user response:

take a look at the dateadd function

DATEADD(DAY, -1, DATE'2021-11-11 10:50:00')

CodePudding user response:

cast the current datetime as date then add the time cast as datetime to the date

declare @date datetime=cast(GetDate()-1 as date)
declare @time time='10:50:00'
select
@date cast(@time as datetime) current_at_10_50


INSERT INTO my_table (name, start_date) VALUES 
('test', @date cast(@time as datetime));
  • Related