Home > Software design >  SQL for Extracting Entries created in last hour
SQL for Extracting Entries created in last hour

Time:07-02

I need a SQL query for extracting all the entries created in the last hour.. but let me explain what "last hour" means. Let's consider that the time is now 13:37. I need to extract all the entries created from 13:00 to 13:37. If I will run the query again at 13:45, the query should show me all the entries created between 13:00 and 13:45. The create date field is unix timestamp format. Many thanks!

CodePudding user response:

Are you using a language to integrate it to the database? if so you can pass in the end timestamp(13:45) and the start timestamp(13:00) as variables and output those records whose timestamp lies in the interval (using the between operator in your SQL query).

CodePudding user response:

It's works in MSSQL

--from Hour
select dateadd(HOUR,DATEPART(HOUR,yourDate),cast(convert(date,yourDate)as datetime))

Sample Trying on 2022-07-01 16:33:07.320

select dateadd(HOUR,DATEPART(HOUR,'2022-07-01 16:32:16.247'),cast(convert(date,'2022-07-01 16:32:16.247')as datetime))

Result will be 2022-07-01 16:00:00.000

  • Related