如何在SQL Server中仅匹配datetime列中的月份和年份部分
我有两个输入,月份和年份如何在SQL Server中仅匹配datetime列中的月份和年份部分,sql,asp.net,sql-server,Sql,Asp.net,Sql Server,我有两个输入,月份和年份 selectedMonth = DDLMonth.SelectedItem.Value;//1 SelectedYear = DDLYear.SelectedItem.Text.Trim();//2013 我希望显示该特定月份和年份的预设记录,而不是仅显示日期。我想要一个注册员工 sql查询 select e.EnquiryId, RegistrationNumber, Name from Branch as b left join Enq as e
selectedMonth = DDLMonth.SelectedItem.Value;//1
SelectedYear = DDLYear.SelectedItem.Text.Trim();//2013
我希望显示该特定月份和年份的预设记录,而不是仅显示日期。我想要一个注册员工
sql查询
select e.EnquiryId, RegistrationNumber, Name
from Branch as b
left join Enq as e on b.Enqui = e.Enqu
where Reg = @RegNo
and Pay=Cheque
如果所选月份=1广告年=2015年,我需要。如果付款日期和支票中有此项。比如:
where Registratio = @RegNo
and Payment(month) = selectedMonth
and payment(year) = selectedyear
and chequere(month) = selected Month
and chequere(year) = selected year
我的chequere和payment date列采用datetime格式。最有效的方法是在应用程序代码中构造一个日期:
selectedMonth = DDLMonth.SelectedItem.Value;//1
SelectedYear = DDLYear.SelectedItem.Text.Trim();//2013
var date = new DateTime(int.Parse(SelectedYear), int.Parse(selectedMonth), 1);
然后将此日期作为参数传递给查询:
where RegistrationNumber = @RegNo
and PaymentDate >= @Date
and paymentdate < DATEADD(MONTH, 1, @Date)
and chequereceiveddate >= @Date
and chequerecieveddate < DATEADD(MONTH, 1, @Date);
其中RegistrationNumber=@RegNo
和PaymentDate>=@Date
和paymentdate=@Date
和chequerecieveddate
这会生成查询,意味着您不需要对列执行
DATEPART()
函数。最有效的方法是在应用程序代码中构造日期:
selectedMonth = DDLMonth.SelectedItem.Value;//1
SelectedYear = DDLYear.SelectedItem.Text.Trim();//2013
var date = new DateTime(int.Parse(SelectedYear), int.Parse(selectedMonth), 1);
然后将此日期作为参数传递给查询:
where RegistrationNumber = @RegNo
and PaymentDate >= @Date
and paymentdate < DATEADD(MONTH, 1, @Date)
and chequereceiveddate >= @Date
and chequerecieveddate < DATEADD(MONTH, 1, @Date);
其中RegistrationNumber=@RegNo
和PaymentDate>=@Date
和paymentdate=@Date
和chequerecieveddate
这会生成查询,意味着您不需要对列执行
DATEPART()
函数。最有效的方法是在应用程序代码中构造日期:
selectedMonth = DDLMonth.SelectedItem.Value;//1
SelectedYear = DDLYear.SelectedItem.Text.Trim();//2013
var date = new DateTime(int.Parse(SelectedYear), int.Parse(selectedMonth), 1);
然后将此日期作为参数传递给查询:
where RegistrationNumber = @RegNo
and PaymentDate >= @Date
and paymentdate < DATEADD(MONTH, 1, @Date)
and chequereceiveddate >= @Date
and chequerecieveddate < DATEADD(MONTH, 1, @Date);
其中RegistrationNumber=@RegNo
和PaymentDate>=@Date
和paymentdate=@Date
和chequerecieveddate
这会生成查询,意味着您不需要对列执行
DATEPART()
函数。最有效的方法是在应用程序代码中构造日期:
selectedMonth = DDLMonth.SelectedItem.Value;//1
SelectedYear = DDLYear.SelectedItem.Text.Trim();//2013
var date = new DateTime(int.Parse(SelectedYear), int.Parse(selectedMonth), 1);
然后将此日期作为参数传递给查询:
where RegistrationNumber = @RegNo
and PaymentDate >= @Date
and paymentdate < DATEADD(MONTH, 1, @Date)
and chequereceiveddate >= @Date
and chequerecieveddate < DATEADD(MONTH, 1, @Date);
其中RegistrationNumber=@RegNo
和PaymentDate>=@Date
和paymentdate=@Date
和chequerecieveddate
这会生成查询,意味着您不需要对列执行
DATEPART()
函数。GarethD提供了最佳解决方案,但另一种方法是使用DATEPART
例如:
where RegistrationNumber = @RegNo
and DATEPART(month, PaymentDate) = selectedMonth
and DATEPART(year, PaymentDate) = selectedYear
and DATEPART(month, chequerecieveddate) = selectedMonth
and DATEPART(year, chequerecieveddate) = selectedYear
这只是给出了一个DATEPART()的示例,如果您想找到某些月份的平均付款额,它会更有用。GarethD提供了最佳解决方案,但另一种方法是使用DATEPART 例如:
where RegistrationNumber = @RegNo
and DATEPART(month, PaymentDate) = selectedMonth
and DATEPART(year, PaymentDate) = selectedYear
and DATEPART(month, chequerecieveddate) = selectedMonth
and DATEPART(year, chequerecieveddate) = selectedYear
这只是给出了一个DATEPART()的示例,如果您想找到某些月份的平均付款额,它会更有用。GarethD提供了最佳解决方案,但另一种方法是使用DATEPART 例如:
where RegistrationNumber = @RegNo
and DATEPART(month, PaymentDate) = selectedMonth
and DATEPART(year, PaymentDate) = selectedYear
and DATEPART(month, chequerecieveddate) = selectedMonth
and DATEPART(year, chequerecieveddate) = selectedYear
这只是给出了一个DATEPART()的示例,如果您想找到某些月份的平均付款额,它会更有用。GarethD提供了最佳解决方案,但另一种方法是使用DATEPART 例如:
where RegistrationNumber = @RegNo
and DATEPART(month, PaymentDate) = selectedMonth
and DATEPART(year, PaymentDate) = selectedYear
and DATEPART(month, chequerecieveddate) = selectedMonth
and DATEPART(year, chequerecieveddate) = selectedYear
这只是给出了一个DATEPART()的示例,如果您想找到某些月份的平均付款额,它会更有用。您好,您尝试过使用DATEPART吗?示例:DATEPART(month,PaymentDate)=selectedMonth和DATEPART(year,PaymentDate)=SelectedYear RegistrationNumber、PaymentDate和ChequerReceiveDate属于哪个表?注册号查询、PaymentDate和ChequerRecieveDate-BranchPaymentsDetails ii没有在那里尝试@dev_JORDHi,您尝试过使用DATEPART吗?示例:DATEPART(month,PaymentDate)=selectedMonth和DATEPART(year,PaymentDate)=SelectedYear RegistrationNumber、PaymentDate和ChequerReceiveDate属于哪个表?注册号查询、PaymentDate和ChequerRecieveDate-BranchPaymentsDetails ii没有在那里尝试@dev_JORDHi,您尝试过使用DATEPART吗?示例:DATEPART(month,PaymentDate)=selectedMonth和DATEPART(year,PaymentDate)=SelectedYear RegistrationNumber、PaymentDate和ChequerReceiveDate属于哪个表?注册号查询、PaymentDate和ChequerRecieveDate-BranchPaymentsDetails ii没有在那里尝试@dev_JORDHi,您尝试过使用DATEPART吗?示例:DATEPART(month,PaymentDate)=selectedMonth和DATEPART(year,PaymentDate)=SelectedYear注册号、PaymentDate和ChequerReceiveDate属于哪个表?注册号查询、PaymentDate和ChequerRecieveDate-BranchPaymentsDetails没有尝试@dev_JORDInteresting方法。我以前从未见过这样做的日期查询,今天我学到了一些东西+1.@dev_JORD您可能没有学到新的东西。我在查询中留下了一些让它毫无意义的部分,它不会编译。我现在已经更正了这个问题。有趣的方法。我以前从未见过这样做的日期查询,今天我学到了一些东西+1.@dev_JORD您可能没有学到新的东西。我在查询中留下了一些让它毫无意义的部分,它不会编译。我现在已经更正了这个问题。有趣的方法。我以前从未见过这样做的日期查询,今天我学到了一些东西+1.@dev_JORD您可能没有学到新的东西。我在查询中留下了一些让它毫无意义的部分,它不会编译。我现在已经更正了这个问题。有趣的方法。我以前从未见过这样做的日期查询,今天我学到了一些东西+1.@dev_JORD您可能没有学到新的东西。我在查询中留下了一些让它毫无意义的部分,它不会编译。我现在已经更正了这个问题。