Date Query for a single Date Range

Alvord, Timothy 276 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

Developer technologies | Transact-SQL
0 comments No comments
{count} votes

Accepted answer
  1. Nasreen Akter 10,811 Reputation points Volunteer Moderator
    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,481 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 421 Reputation points
    2021-02-23T17:27:42.503+00:00

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


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.