如何在sql server c中逐个搜索日期#

时间:2014-02-11 08:35:25

标签: c# sql-server

我在数据库GRP(PK),start_dateend_date中有列。start_date / end_date具有数据类型日期。

我有一个datagridview,一个包含12个月的组合框和一个按月搜索记录的按钮。怎么做,我不知道。

有人可以给我这个想法。我想在今年也这样做。

感谢。

4 个答案:

答案 0 :(得分:0)

尝试使用以下代码:

declare @StartDate date
set @StartDate = getdate()
select @StartDate 

Select MONTH(@StartDate)

SELECT *
FROM YOURTABLE
WHERE MONTH(StartDate) = @Month

答案 1 :(得分:0)

如果您使用的是ado.net,请查看Transact-SQL Month。如果应用程序变得更复杂,我建议使用实体框架。

Select * from table where Month(StartDate) = @Month

答案 2 :(得分:0)

您可以在查询中使用datepart() TSQL函数, 例如,如果您只想加载第二个月的记录:

SELECT *   FROM leaves WHERE DATEPART(month, start_date)=2 or DATEPART(month, end_date)=2

答案 3 :(得分:0)

更改您的查询,如

string _query = String.Format("select * from leaves where Month(start_date) ='{0}' and Month(end_date) ='{0}'",yourComboxBoxSelectedValue);

objcmd = new SqlCommand(_query, objc);