data列的类型是varchar(50),然后格式为2013-05-07,我想每次执行的时候,都查询的是昨天的数据,我在网上找的代码select * from test where datediff('d',Date,now)=1 提示我datediff 指定的参数 1 无效。出错,不知道这语句应该怎么写?请指教
declare @begindate as varchar(50)=convert(varchar(50),getdate()-1,120) @enddate as varchar(50)=convert(varchar(50),getdate(),120) select * from test where date>=@begindate and date<@enddate /* 多写点语句,并不要怕麻烦。这样你的参数不管是几, 下次都会是同样的执行计划。参数化。 用函数看上去很帅气。但是它的执行效率并不会比 这么长一段代码高。 */
where datediff(d,[Date],getdate())=1
这么写
CreateTime就是你的date字段我在数据库运行了的 可以的
提示:从字符串向 datetime 转换时失败。
where datediff(d,cast([Date] as date),getdate())=1
declare
@begindate as varchar(50)=convert(varchar(50),getdate()-1,120)
@enddate as varchar(50)=convert(varchar(50),getdate(),120)
select * from test
where date>=@begindate
and date<@enddate
/*
多写点语句,并不要怕麻烦。这样你的参数不管是几,
下次都会是同样的执行计划。参数化。
用函数看上去很帅气。但是它的执行效率并不会比
这么长一段代码高。
*/