'How can I get a specific time of a date using SQL code
How can I get a specific time of a date using SQL code?
Today is 02.05.2022 16:45:00 using ->
SELECT GETDATE()
What I need is 03.05.2022 8:00:00 -> ?
What would be the SQL code for it?
Thanks
EDIT
I can get the tomorrow's date using ->
SELECT DATEADD(day, 1, GETDATE())
but I actually need the date of tomorrow at 8 o'clock.
Solution 1:[1]
SELECT DATEADD(HOUR, 8, CAST(CAST(GETDATE()+1 AS DATE) AS DATETIME))
Solution 2:[2]
Today is 02.05.2022 16:45:00 using -> SELECT GETDATE() What i need is 03.05.2022 8:00:00 -> ?
Given your required date is "tomorrow" with no other narrative of requirements, the following is maybe what you are expecting:
select Convert(datetime,Convert(date,DateAdd(day,1,GetDate()))) + Convert(datetime, '08:00')
Solution 3:[3]
Assuming SQL Server, try this:
select convert(datetime,'03/05/2022 8:00:00')
Solution 4:[4]
Try
SELECT CAST('2022-05-03 8:00:00' AS DATETIME) AS NewDate
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Jonas Metzler |
| Solution 2 | Stu |
| Solution 3 | Sparky |
| Solution 4 | General Grievance |
