Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

get the date and time for today at midnight and add to it

Is there a sql command to get today's date at midnight and be able to add a number of minutes to it?

like image 939
user867621 Avatar asked Oct 08 '11 22:10

user867621


People also ask

How do I get midnight time in SQL?

This works, because in SQL-server, datetime is a float value, the integer part representing the days from 01.01. 1900, the sub-integer part represents the percentage of the full day (24h). So if you round-down (floor) the floating-point number to a integer, you get the midnight-time. 1900-01-01T00:00:00.000 being 0.

How do I get time from Getdate?

The GETDATE() function returns the current database system date and time, in a 'YYYY-MM-DD hh:mm:ss. mmm' format. Tip: Also look at the CURRENT_TIMESTAMP function.


2 Answers

Date now midnight time:

Select DATEADD(d,0,DATEDIFF(d,0,GETDATE())) 

ADD 600 Minutes:

Select DATEADD(mi,600,DATEDIFF(d,0,GETDATE())) 
like image 163
Carrisi Avatar answered Oct 12 '22 02:10

Carrisi


Yes, just use datediff and dateadd functions to strip the time from any date, then add a fractional portion of a day to that number

Declare @aDate DateTime Set @aDate = getDate() Declare @Minutes Integer Set @minutes = 600 -- 10 hours  Select DateAdd(day, DateDiff(day, 0, @aDate), 0) + @minutes / 1440.0  -- 1440 min/day  -- or You could also use the dateadd again... Select DateAdd(minute, @minutes , DateAdd(day, DateDiff(day, 0, @aDate), 0)) 

Both selects return 10:00 am on the same day (more or less). This works because of, well, check out this SO answer

EDIT: Added sample script to show how this works:

declare @dtTim datetime = getDate() declare @today datetime = dateAdd(day, dateDiff(day, 0, @dtTim ), 0) select  @dtTim, @today 
like image 36
Charles Bretana Avatar answered Oct 12 '22 01:10

Charles Bretana