Skip to content
Advertisement

Timeserial not recognized built in function in MS SQL

I am trying to implement an SQL query that gets records between today’s fixed timing 18:00 and yesterday’s fixed timing 18:00 based on a Date time column that I have in my table.

I tried this query

DECLARE @today date = GETDATE()
SELECT * 
FROM mytab
WHERE datetimecolumn Between @today-1 + TimeSerial(18,0,0) 
                              And @today   + TimeSerial(18,0,0)

But, it’s throwing an error Timeserial is not a recognized built-in function name.

Any ideas please?

Advertisement

Answer

It would be best to use the DATEADD() function to add the time to the date you want. In this example, it subtracts 6 hours from the first number to get 18:00 yesterday, and then adds 18 hours to get 18:00 today.

DECLARE @today date = GETDATE();
SELECT * FROM mytab WHERE datetimecolumn Between DATEADD(hour,-6,@today) And DATEADD(hour,18,@today);
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement