简体   繁体   English

SQL日期范围查询

[英]SQL Date Range Query

I have a SQL2005 database with a table that has a field that is in datetime format and hold the date and time that a record was added. 我有一个带有表的SQL2005数据库,该表具有日期时间格式的字段,并保存添加记录的日期和时间。 A typical entry looks like 10/03/2011 13:35:15 一个典型的条目看起来像是10/03/2011 13:35:15

How do I query the table for records using a date range from ad asp page? 如何使用广告asp页面中的日期范围在表格中查询记录?

The between operator should be able to do the trick. between运算符应该能够解决问题。

select 
    * 

from YourTable where DateField between '10/1/2011' and '10/5/2011 11:59:59 PM'

This will get you all the rows in your table for a give Start Date (@StartDate) and End Date (@EndDate): 这将为您提供表中的所有行,以指定开始日期(@StartDate)和结束日期(@EndDate):

 select 
    *
 from 
    [TableName]
 where
    [DateToTest] BETWEEN @StartDate AND @EndDate

Just replace [TableName] and [DateToTest] with your values. 只需将[TableName]和[DateToTest]替换为您的值即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM