Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add minutes to the time part of datetime

How to add minutes(INT) to the time part of datetime ?

For example :

If i have datetime variable like this :

  @shift_start_time =  2015-11-01 08:00:00.000    @increase = 30 

How to get the result :

2015-11-01 08:30:00.000 
like image 566
Anyname Donotcare Avatar asked Nov 17 '15 15:11

Anyname Donotcare


People also ask

How do you add minutes to a date time?

The DateTime. AddMinutes() method in C# is used to add the specified number of minutes to the value of this instance. It returns the new DateTime.

How do I add 30 minutes to time in SQL?

To add minutes to a datetime you can use DATE_ADD() function from MySQL. In PHP, you can use strtotime(). select date_add(yourColumnName,interval 30 minute) from yourTableName; To use the above syntax, let us create a table.

How can I add minutes to current time in SQL?

We can use DATEADD() function like below to add minutes to DateTime in Sql Server. DATEADD() functions first parameter value can be minute or mi or n all will return the same result.

How do I add hours minutes seconds to a datetime in SQL Server?

DECLARE @dt DATETIME = '2021-12-31 00:00:00.000' SELECT DATETIMEFROMPARTS( DATEPART(YEAR, @dt), DATEPART(MONTH, @dt), DATEPART(DAY, @dt), 23, /* hour */ 59, /* minute */ 59, /* second */ 0 /* fractional seconds*/ );


2 Answers

Use DATEADD:

SELECT DATEADD(mi, @increase,   @shift_start_time); 

db<>fiddle demo

like image 165
Lukasz Szozda Avatar answered Sep 26 '22 02:09

Lukasz Szozda


Using dateadd:

DATEADD(minute,@increase,@shift_start_time) 

the first argument can be chosen among: year quarter month dayofyear day week weekday hour minute second millisecond microsecond nanosecond

please check https://msdn.microsoft.com/it-it/library/ms186819%28v=sql.120%29.aspx

like image 40
Simone Avatar answered Sep 26 '22 02:09

Simone