Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Any ideas please?

>Solution :

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);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading