Date Query for a single Date Range

Alvord, Timothy 236 Reputation points
2021-02-23T16:53:20.69+00:00

Hi,
The Date field in the Table is actually a DateTime field. I need to be able to do either of the following Queries:

SELECT Date FROM tblHistory
WHERE Date BETWEEN '2021-02-22' AND '2021-02-22'

or

SELECT Date FROM tblHistory
WHERE Date LIKE '2021-02-22%'

Neither option returns any rows for a single date

Transact-SQL
Transact-SQL
A Microsoft extension to the ANSI SQL language that includes procedural programming, local variables, and various support functions.
4,552 questions
0 comments No comments
{count} votes

Accepted answer
  1. Nasreen Akter 10,736 Reputation points
    2021-02-23T17:44:13.697+00:00

    Hi @Alvord, Timothy ,

    Please try the following:

    SELECT Date FROM tblHistory   
    WHERE FORMAT(Date, 'yyyy-MM-dd') = '2021-02-22'  
    

    or, if you want to use between, then

    SELECT Date FROM tblHistory   
    WHERE FORMAT(Date, 'yyyy-MM-dd') BETWEEN '2021-02-22' AND '2021-02-23'  
    

    ----------

    If the above response is helpful, please accept as answer and up-vote it. Thanks!

    0 comments No comments

2 additional answers

Sort by: Most helpful
  1. Tom Cooper 8,466 Reputation points
    2021-02-23T17:07:46.027+00:00

    WHERE Date >= '2021-02-22' AND Date < '2021-02-23'

    Tom

    0 comments No comments

  2. Farhan Jamil 416 Reputation points
    2021-02-23T17:27:42.503+00:00

    SELECT Date FROM tblHistory
    WHERE Date BETWEEN '20210222' AND '20210223'