Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Construct DateTime using today's date at a specific time

I'd like to get 4:30 PM of the current day. Hard-coding this way doesn't work:

SELECT '07242012 16:30:00.000'

This is proving to be more difficult than I thought it would be. How do I approach this?

like image 748
John Zumbrum Avatar asked Jul 24 '12 16:07

John Zumbrum


1 Answers

SQL Server 2000 / 2005:

SELECT DATEADD(MINUTE, 30, DATEADD(HOUR, 16, DATEDIFF(DAY, 0, CURRENT_TIMESTAMP)));

-- or

SELECT DATEADD(MINUTE, (16*60) + 30, DATEDIFF(DAY, 0, CURRENT_TIMESTAMP))

-- or

SELECT CONVERT(DATETIME, CONVERT(CHAR(9), CURRENT_TIMESTAMP, 112) + '16:30');

SQL Server 2008+:

SELECT CONVERT(DATETIME, CONVERT(DATE, CURRENT_TIMESTAMP)) + '16:30';

SQL Server 2012:

SELECT SMALLDATETIMEFROMPARTS(YEAR(GETDATE()), MONTH(GETDATE()), DAY(GETDATE()), 16, 30);
like image 187
Aaron Bertrand Avatar answered Oct 13 '22 23:10

Aaron Bertrand