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

Recently, I worked on a requirement to retrieve the list of Sales order dates for specific time components. I used the DATEPART function, but after executing the query, I got this error.

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

I executed the SQL query below to retrieve the Sales table record list.

Select * from sales
WHERE DATEPART(HOUR, sale_Date) BETWEEN 9 AND 16

But, after executing the above query, I got the above error as shown in the screenshot below.

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

Solution

To fix this issue, we need to do a double cast after getting the current time using the following query.

Select * from sales
WHERE DATEPART(HOUR, cast(cast(sale_Date as date) as datetime)) BETWEEN 9 AND 16

After executing the above query, I got the expected output without any error as shown in the screenshot below.

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

Video Tutorial

You may also like the following articles.