我正在使用sql select命令选择表中日期介于两个值之间的所有记录

时间:2013-01-04 21:26:23

标签: asp.net vb.net sqlcommand

这就是我所拥有的,但目前它不起作用..

帮助?

sqlGVQuarterly.SelectCommand = "SELECT [CleaningID], [WorkType], [QuarterSection],
[BasinID], [RecordDate], [TotalFootage], [H2O], [GritRemoved], [StartingStreet],
[LocationPurpose], [Operators], [JobHours], [LaborHours], [ChaseTruckHours],
[VacTruckHours], [JetTruckHours] FROM [SanSewerCleaning] 
WHERE [RecordDate] BETWEEN '1/1/ + ddlSelectYear.SelectedValue' AND '3/31/+ ddlSelectYear.SelectedValue'"

5 个答案:

答案 0 :(得分:2)

您正在错误地构建字符串:

sqlGVQuarterly.SelectCommand = "SELECT [CleaningID], [WorkType], [QuarterSection],
[BasinID], [RecordDate], [TotalFootage], [H2O], [GritRemoved], [StartingStreet],
[LocationPurpose], [Operators], [JobHours], [LaborHours], [ChaseTruckHours],
[VacTruckHours], [JetTruckHours] FROM [SanSewerCleaning] 
WHERE [RecordDate] BETWEEN '1/1/"  + ddlSelectYear.SelectedValue +
"' AND '3/31/" + ddlSelectYear.SelectedValue + "'";

答案 1 :(得分:0)

您正在发送字符串文字“ddlSelectYear.SelectedValue”。确保在“1/1 /”之后和“AND”之前有双引号。

您的代码应如下所示:

sqlGVQuarterly.SelectCommand = "SELECT [CleaningID], [WorkType], [QuarterSection],
[BasinID], [RecordDate], [TotalFootage], [H2O], [GritRemoved], [StartingStreet],
[LocationPurpose], [Operators], [JobHours], [LaborHours], [ChaseTruckHours],
[VacTruckHours], [JetTruckHours] FROM [SanSewerCleaning] 
WHERE [RecordDate] BETWEEN '1/1/" + ddlSelectYear.SelectedValue+"' AND '3/31/"+ ddlSelectYear.SelectedValue+"'";

答案 2 :(得分:0)

您正在使查询字符串错误。我想你想要这样的东西:

sqlGVQuarterly.SelectCommand = "SELECT [CleaningID], [WorkType], [QuarterSection],
[BasinID], [RecordDate], [TotalFootage], [H2O], [GritRemoved], [StartingStreet],
[LocationPurpose], [Operators], [JobHours], [LaborHours], [ChaseTruckHours],
[VacTruckHours], [JetTruckHours] FROM [SanSewerCleaning] 
WHERE [RecordDate] BETWEEN '1/1/" + ddlSelectYear.SelectedValue + "' 
AND '3/31/" + ddlSelectYear.SelectedValue+"'";

因为SQL引擎中不存在值ddlSelectYear.SelectedValue,所以会引发语法错误

答案 3 :(得分:0)

我也会使用yyyy-M-dd模式,所以:

WHERE [RecordDate] BETWEEN '" + ddlSelectYear.SelectedValue + "-01-01' AND '" + ddlSelectYear.SelectedValue + "-03-31'"

答案 4 :(得分:0)

此外,如果您的时间/日期值默认为午夜,您将获得:

开始:1/1 / YYYY 00:00:00 结束时间:3/31 / YYYY 00:00:00

任何时间戳在3月31日午夜之后的记录都不会包含在您选择的记录中。