Asp.net 通配符sql日期查询
我在做一个项目,一直在做一个sql查询,查询没有给出任何错误,也没有返回任何结果。告诉我问题出在哪里Asp.net 通配符sql日期查询,asp.net,sql,sql-server,Asp.net,Sql,Sql Server,我在做一个项目,一直在做一个sql查询,查询没有给出任何错误,也没有返回任何结果。告诉我问题出在哪里 SELECT barcode , Date , timein , timeout , totaltime , leave , remarks FROM TimeSheet WHERE barcode = @barcode AND Date LIKE '@year-07-%' 我在运行时@barcode@year以变量形式传递
SELECT
barcode
, Date
, timein
, timeout
, totaltime
, leave
, remarks
FROM TimeSheet
WHERE barcode = @barcode
AND Date LIKE '@year-07-%'
我在运行时@barcode@year以变量形式传递2个值
但是,如果我在sql编辑器中显式地使用values类型运行查询,它将正常工作并返回值
如果你运行这个
SELECT
barcode
, Date
, timein
, timeout
, totaltime
, leave
, remarks
FROM TimeSheet
WHERE barcode = 123456
AND Date LIKE '2013-07-%'
它返回值SQL Server不会展开'@year-07-%'中的变量 假设@year参数是一个varchar,[date]列是一个日期,您可以尝试以下方法:
where convert(varchar(10), [date], 120) like @year + '-07-%'
或者更好:
where datepart(year, [date]) = cast(@year as int)
and datepart(month, [date]) = 7
SQL Server不展开“@year-07-%”中的变量 假设@year参数是一个varchar,[date]列是一个日期,您可以尝试以下方法:
where convert(varchar(10), [date], 120) like @year + '-07-%'
或者更好:
where datepart(year, [date]) = cast(@year as int)
and datepart(month, [date]) = 7
如果有“日期”列上的索引,则以下内容将是有效的
SELECT
barcode
, Date
, timein
, timeout
, totaltime
, leave
, remarks
FROM TimeSheet
WHERE barcode = @barcode
AND Date >=dateadd(month,6,dateadd(year,@year-1900,0))
AND Date <dateadd(month,7,dateadd(year,@year-1900,0))
如果有“日期”列上的索引,则以下内容将是有效的
SELECT
barcode
, Date
, timein
, timeout
, totaltime
, leave
, remarks
FROM TimeSheet
WHERE barcode = @barcode
AND Date >=dateadd(month,6,dateadd(year,@year-1900,0))
AND Date <dateadd(month,7,dateadd(year,@year-1900,0))
这是安多玛的建议
create table #timesheet (barcode int, entrydate date, other_col varchar(20))
insert into #timesheet (barcode, entrydate , other_col)
values (123456,'2013-07-01','helloA')
,(123456,'2013-07-02','helloB')
,(123457,'2013-07-02','helloC')
,(123456,'2013-06-01','helloD')
DECLARE @YEAR VARCHAR(4) = '2013'
,@barcode int = 123456
Select *
From #timesheet
Where barcode = @barcode
And convert(varchar(10), entrydate, 120) like @year + '-07-%'
Select *
From #timesheet
Where barcode = @barcode
And datepart(year,entrydate) = cast(@year as int)
And datepart(month,entrydate) = 7
这是安多玛的建议
create table #timesheet (barcode int, entrydate date, other_col varchar(20))
insert into #timesheet (barcode, entrydate , other_col)
values (123456,'2013-07-01','helloA')
,(123456,'2013-07-02','helloB')
,(123457,'2013-07-02','helloC')
,(123456,'2013-06-01','helloD')
DECLARE @YEAR VARCHAR(4) = '2013'
,@barcode int = 123456
Select *
From #timesheet
Where barcode = @barcode
And convert(varchar(10), entrydate, 120) like @year + '-07-%'
Select *
From #timesheet
Where barcode = @barcode
And datepart(year,entrydate) = cast(@year as int)
And datepart(month,entrydate) = 7
wht是ur codebehind是vb还是CW是日期列的数据类型?Date列的datetype是Date是ur codebehind是vb还是CW是日期列的数据类型?Date列的datetype仍然不显示任何数据类型result@MohsinMustufa-声明的@year的数据类型是什么?【日期】的数据类型是什么column?[date]列的数据类型为DateBuddy感谢它现在完成了实际上gridview在更新面板中,它的可见性为false这就是为什么它不显示任何记录仍然不显示任何记录的原因result@MohsinMustufa@year的声明数据类型是什么?[date]列的数据类型是什么?[date]的数据类型列是Date的,感谢它现在完成了实际上gridview在更新面板中,它的可见性为false,这就是为什么它不显示任何记录相同的结果不返回任何记录value@MohsinMustufa然后测试你的数据。这将工作并执行相同的结果,不返回任何value@MohsinMustufa然后测试你的数据。这将在sqlfiddle中创建模式时非常有用在sqlfiddle中创建模式时非常有用