Conrad Conrad - 1 year ago 73
SQL Question

How to create a time of 9pm today in SQL

I am trying to get a

value of "9pm today" in a query. I thought I could use


but SQL Server doesn't like that - I get the error

The datepart hour is not supported by date function dateadd for data
type date.

Suggestions for a workaround?

Answer Source

Pretty simple, just cast date back to datetime after casting to date.

Thus you'll get current_date 00:00:00 and then add 21 hours:

select dateadd(hh, 21, cast(cast(getdate() as date) as datetime))