Milad
Milad

Reputation: 125

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.

Upvotes: 0

Views: 87

Answers (4)

Mauricio Atanache
Mauricio Atanache

Reputation: 2581

Try

SELECT CAST('2022-05-03 8:00:00' AS DATETIME) AS NewDate

Upvotes: 0

Jonas Metzler
Jonas Metzler

Reputation: 5975

SELECT DATEADD(HOUR, 8, CAST(CAST(GETDATE()+1 AS DATE) AS DATETIME))

Upvotes: 2

Stu
Stu

Reputation: 32579

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')

Upvotes: 1

Sparky
Sparky

Reputation: 15085

Assuming SQL Server, try this:

select convert(datetime,'03/05/2022 8:00:00')

Upvotes: 0

Related Questions