Simple DateTime sql query

前端 未结 8 545
借酒劲吻你
借酒劲吻你 2020-12-07 21:42

How do I query DateTime database field within a certain range?

I am using SQL SERVER 2005

Error code below

SELECT * 
  FROM TABLENAME 
 WHERE         


        
8条回答
  •  情歌与酒
    2020-12-07 22:20

    You need quotes around the string you're trying to pass off as a date, and you can also use BETWEEN here:

     SELECT *
       FROM TABLENAME
      WHERE DateTime BETWEEN '04/12/2011 12:00:00 AM' AND '05/25/2011 3:53:04 AM'
    

    See answer to the following question for examples on how to explicitly convert strings to dates while specifying the format:

    Sql Server string to date conversion

提交回复
热议问题