Simple DateTime sql query

前端 未结 8 543
借酒劲吻你
借酒劲吻你 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:28

    Others have already said that date literals in SQL Server require being surrounded with single quotes, but I wanted to add that you can solve your month/day mixup problem two ways (that is, the problem where 25 is seen as the month and 5 the day) :

    1. Use an explicit Convert(datetime, 'datevalue', style) where style is one of the numeric style codes, see Cast and Convert. The style parameter isn't just for converting dates to strings but also for determining how strings are parsed to dates.

    2. Use a region-independent format for dates stored as strings. The one I use is 'yyyymmdd hh:mm:ss', or consider ISO format, yyyy-mm-ddThh:mi:ss.mmm. Based on experimentation, there are NO other language-invariant format string. (Though I think you can include time zone at the end, see the above link).

提交回复
热议问题