在asp.net中从当前日期获取月份和年份的条款

时间:2018-05-24 05:09:09

标签: asp.net vb.net

我想从sql数据库按月和年份过滤数据。我将当前日期设置为(yyyy-MM-dd)格式。从那里我想按月和年过滤。以下是我的源代码。

     Dim ServerDate As String
        ServerDate = Date.Now.ToString("yyyy-MM-dd")
        Dim nowYear As Integer
        nowYear = Date.Now.Year
        Dim nowMonth As Integer
        nowMonth = Date.Now.Month

这是我的选择声明:

 cmd.CommandText = "Select COUNT(*) FROM ScanningData WHERE ASSET_NUMBER=" & AssetTxt.Text & " And LOC_DEPT=" & DeptCodeTxt.Text & " AND UNIT_NO=" & UnitNoTxt.Text & " AND UPDATE_DATE='" & nowMonth & nowYear & "';"    

有人可以帮我解决这个问题。提前谢谢

1 个答案:

答案 0 :(得分:0)

使用DATEPART

例如(未测试语法错误)

Select COUNT(*) FROM ScanningData 
WHERE ASSET_NUMBER=" & AssetTxt.Text & " And LOC_DEPT=" & DeptCodeTxt.Text & " 
AND UNIT_NO=" & UnitNoTxt.Text & " AND ( DATEPART(mm, CreationDate) = "& nowMonth & ") and (DATEPART(yy, CreationDate)="& nowYear  & " )