• Register
0 votes
29 views

SQL query between two dates and times

I need to select the records between two specific dates and times. Any idea?
1.9k points

Please log in or register to answer this question.

1 Answer

0 votes

SQL query between two dates and times

If you execute a query and it has between dates, and your DateTime has different times, not all relevant data will be returned. IT happens because you did not specify the correct time for the date-time. 

 If there is a [time] field in your table, a DateTime variable, the format is like 2020-11-08 23:55:00.000. You need to select the records between two specific dates and times.

If you write the query like this:

SELECT * FROM  [table] 
WHERE time >=2020-11-08 23:53:00.000 AND time <= 2020-11-08 23:58:00.000

Your query will not work.  You will receive the error code: Incorrect syntax. What is wrong with your query? The answer is simple: the date format is incorrect. Here is the correct syntax:

SELECT * FROM  [table] 
WHERE [time] >='2020-11-08 23:53:00.000' AND [time] <= '2020-11-08 23:58:00.000'

The format yyy-MM-dd HH:mm:ss.fff is a convenience shown by the query tool. You need to out DateTime values in quotes. I also recommend putting brackets around your time column name. It will not be treated as a restricted term.

SELECT * FROM  [table] 
WHERE [time] >='2014-04-08 23:53:00' AND [time] <= '2014-04-08 23:58:00'

Here I specify >= and <=. I can simplify the statement using the operator BETWEEN.

SELECT * FROM  [table]  
WHERE [time]  BETWEEN '2014-04-08 23:53:00' AND '2014-04-08 23:58:00'

I hope it helps!

3.4k points

Related questions

0 votes
1 answer 6 views
6 views
Problem: Recursive query in sql server without cte.
asked 6 days ago Shima 106k points
0 votes
1 answer 34 views
34 views asked Oct 23, 2020 miki 1.9k points
0 votes
1 answer 574 views
574 views
Problem: I have a City table and I want to query all the cities of Pakistan that have the population larger than 200000 and their names start in H. Country code for Pakistan is Pak. How can I do that using SQL?
asked Oct 18, 2020 Code Learner 10.5k points
0 votes
1 answer 45 views
45 views
Problem: Having a tough time with this, help please : Write a query to display the names of all users that start and end with letter a.
asked Jun 14 nila 104k points
0 votes
1 answer 21 views
21 views
Problem: How to > write a query to display all 6-letter names that start with p and end with a
asked Mar 16 Azlan2020 2.5k points
0 votes
1 answer 20 views
20 views
Problem: Is anyone available to assist me and resolve this issue? Sql query to get data between two dates
asked May 8 Ifra 43.4k points
0 votes
1 answer 22 views
22 views
Problem: display the total row for this query.
asked Apr 10 Dan phillip 4.8k points
1 vote
1 answer 5 views
5 views
Problem: Please help me with this Java query string value. Url with %s in place of query.
asked Mar 25 PkGuy 25.9k points
0 votes
1 answer 524 views
524 views
Problem: write a query to display the block number in which the maximum number of departments is located.
asked Mar 16 Azlan2020 2.5k points
0 votes
1 answer 52 views
52 views
Problem: How to> write a query to display the number of sales that were made in the last 40 months
asked Mar 16 Azlan2020 2.5k points