[英]Query to return data from SQL Server table based on date criteria
I have the following piece of code in C# that I use to select some rows from a SQL Server table based on a date criteria. 我有以下C#代码段,用于根据日期条件从SQL Server表中选择一些行。
DateTime From, DateTime To
SqlParameter[] oParam = new SqlParameter[3];
oParam[0] = new SqlParameter("@From", From.Date);
oParam[1] = new SqlParameter("@To", To.Date);
DataTable dt = clsDatabaseHistory.ExecuteReader("SELECT * FROM tblHistory WHERE Date_Requested BETWEEN @From and @To", oParam);
If for example From=18/08/2011
and To=18/08/2011
and there is data in the table tblHistory
that has the Date_Requested
value as 18/08/2011
the query does not return it. 例如,如果
From=18/08/2011
18/08/2011
和To=18/08/2011
18/08/2011
且表tblHistory
中的数据的Date_Requested
值为18/08/2011
则查询不会返回该数据。
But if I change the value of To
from 18/08/2011
to To=19/08/2011
the query returns me all the values from the table that have a Date_Requested
value of 18/08/2011
, but none from the 19/08/2011
. 但是,如果我将
To
的值从18/08/2011
更改To=19/08/2011
Date_Requested
,则查询将返回表中所有Date_Requested
值为18/08/2011
值,但不会从19/08/2011
。
How can something like that be possible and what query should I use to return rows where the date field is between date1 and date2. 这样的事情怎么可能发生,我应该使用什么查询来返回日期字段在date1和date2之间的行。
Something like : 就像是 :
select * rows where datevalue >= date1 and datevalue <= date2
Thank you. 谢谢。
From.Date
and To.Date
gets the date portion on c# side ignoring the time portion; From.Date
和To.Date
在c#端获取日期部分,而忽略时间部分; you need to do something similar on the database side. 您需要在数据库端执行类似的操作。
Try 尝试
"SELECT * FROM tblHistory WHERE cast(Date_Requested as DATE) BETWEEN @From and @To"
to remove the time portion. 删除时间部分。
EDIT: as explained in this answer , you could change @To
param value to 编辑:如此答案中所述 ,您可以将
@To
参数值更改为
oParam[1] = new SqlParameter("@To", To.Date.AddDays(1));
I bet your Date_Requested in your table also has some time associated with it - so it probably really is 18/08/2011 14:37
or something like that. 我敢打赌,您在表中的Date_Requested也有一些时间与之相关-所以它可能实际上是
18/08/2011 14:37
或类似的时间。
The BETWEEN
clause will be selecting anything between 18/08/2011 00:00:00
and 18/08/2011 00:00:00
- basically nothing. BETWEEN
子句将在18/08/2011 00:00:00
到18/08/2011 00:00:00
之间选择任何内容-基本上什么也没有。
What you need to take into account when working with DATETIME
is the fact there's always also TIME
involved! 使用
DATETIME
时需要考虑的是,事实总是也涉及到TIME
!
If you want everything for today, you need to use: 如果您今天想要所有东西,则需要使用:
BETWEEN `18/08/2011 00:00:00` AND `18/08/2011 23:59:59`
With those two values, you should get all rows with a Date_Requested
of today. 使用这两个值,您应该获得带有
Date_Requested
今天的所有行。
OR: in SQL Server 2008 and newer, you could also use the DATE
column type which stores date only - no time portion involved. 或:在SQL Server 2008和更高版本中,您还可以使用
DATE
列类型,该类型仅存储日期-不涉及时间部分。 In that case, you should be fine with your query values. 在这种情况下,您应该可以使用查询值。
更改查询以使用> =和<
select * rows where datevalue >= date1 and datevalue < date2 + 1
You need to account for time (not just date). 您需要考虑时间(不仅仅是日期)。 One way I handle that is like this:
我处理的一种方式是这样的:
SELECT
...
WHERE CONVERT(varchar(8), date_begin, 112) <= convert(varchar(8), @to, 112)
This converts dates to YYYYMMDD
format (with no time), which is very easy to use in <, >, =
comparrisons. 它将日期转换为
YYYYMMDD
格式(没有时间),在<, >, =
比较中非常容易使用。
Ok, thanks to you all, i've done the following thing 好的,谢谢大家,我做了以下事情
oParam[2] = new SqlParameter("@To", To.Date.AddDays(1));
and used the following select 并使用以下选择
SELECT * from MyTable WHERE CONVERT(varchar(8), Date_Requested, 112) >= CONVERT(varchar(8), @From, 112) and CONVERT(varchar(8), Date_Requested, 112) < CONVERT(varchar(8), @To, 112)
Datetime variables must be converted to be compared. 日期时间变量必须进行转换才能进行比较。
Thanks all! 谢谢大家!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.