Home > Blockchain >  How to get rows in a table that start from a specific date and time in SQL?
How to get rows in a table that start from a specific date and time in SQL?

Time:12-16

In one of my SQL Server tables, I have te following rows:

Date Time
2021-10-01 10:00:00.0000000
2021-10-01 11:00:00.0000000
2021-10-01 12:00:00.0000000
2021-10-02 10:00:00.0000000
2021-10-02 11:00:00.0000000
2021-10-02 12:00:00.0000000
2021-10-03 10:00:00.0000000
2021-10-03 11:00:00.0000000
2021-10-03 12:00:00.0000000

I'd like to get rows that start from a specific Date, and then from a specific Time. For instance, passing 2021-10-02 and 12:00:00.0000000, I'd like to get the last four rows because 2021-10-02 is considered the "starting date", and then 12:00:00.0000000 is considered the "starting time". Unfortunately, I'm not so good at SQL. I guess nested queries should be the way to go.

CodePudding user response:

You'll need to be a little "2 pronged" about this. You'll need to check the date is on 2021-10-02 and on or after 12:00:00.0000000, or on or after 2021-10-03:

SELECT {YourColumns}
FROM dbo.YourTable
WHERE (YourDate = '20210102' AND YourTime >= '12:00:00')
   OR YourDate >= '20210103';
  • Related