简体   繁体   English

查询以根据日期条件从SQL Server表返回数据

[英]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/2011To=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.DateTo.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:0018/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.

相关问题 查询以基于此多个条件从表中选择值列表 - Query to select a list of values from a table based on this multiple criteria 查询显示sql服务器中表中存在数据,但不显示任何受影响的行并始终返回false - query shows data exists in the table in the sql server but it shows no row affected and return false always 如何从SQL Server中的表中循环数据以及如何使用返回的数据查询另一个表。 请参见 - How to loop data from a table in sql server and using the returned data to query another table. Please see 根据过滤条件生成动态 SQL 查询 - Generate Dynamic SQL Query based on Filter Criteria 从不可解析的表SQL Server查询和导出 - Query and export from unsortable table SQL Server 如果找不到数据,SQL Server查询将连续返回“无数据” - SQL Server Query return “No data” in a row if no data found 如何从c#中的特定日期的sql server 2008表中访问数据? - How to access data from sql server 2008 table of a specific date in c#? 无法根据SQL Server表中的列从Excel文件中读取数据 - Unable to read data from excel file based on the columns in SQL Server table 选择查询以从 SQL Server 获取数据 - Select query to get data from SQL Server 如何根据日期和时间从SQL查询到GridView进行排序 - How to sort based on date and time from a sql query into a GridView
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM