sql - SQL Server:日期不一致
问题描述
create table #temp
(
A date
)
insert into #temp(A)
values(GETDATE())
insert into #temp(A)
values(GETDATE()-1)
现在,当我将表查询为
select A from #temp where A>=GETDATE() and A<=GETDATE()
我没有得到任何记录但是GETDATE()
记录值应该满足我的 where 条件,它不应该至少传递给我一个记录吗?
如果我在这里遗漏了一些要点,请指导我。
解决方案
您需要进行转换,所以看起来:
where a >= convert(date, dateadd(day, -1, getdate())) and
a <= convert(date, getdate());
您的where
条款比较日期为:
where a >= '2020-04-21 16:01:27.277' and a <= '2020-04-21 16:01:27.277'
因此,您需要转换日期,因为getdate()
还会返回时间部分。
由于您的where
子句查找单日,因此您可以这样做:
where a = convert(date, getdate())
推荐阅读
- asp.net-identity - ASP.NET Core 3.0 - 身份自定义问题
- angular - 发布新数据后,是否可以从 web api 角度获取当前 id 的值?
- java - 将 Arrays.sort() 用于非字母字符,例如 {'+' '1' 'D'}
- amazon-web-services - 是否可以使用/连接 AWS Cognito 用户池到 AWS SFTP 服务器访问
- javascript - 调整浏览器窗口大小时如何显示带点的文本?
- flutter - 如何在 Flutter 中匹配密码和确认密码?
- python - 在 Python 中迭代 OSMNX 中的等时线
- flutter - 来自 api 的表情符号无法正确显示
- javascript - 忽略 jshint 中特定变量的驼峰式检查
- wordpress - MVC 方式的 WordrPess 插件