sqlserver 时间测试

select * from GropBy where  [date] BETWEEN '2010-10' and '2015-10' --从字符串转换日期和/或时间时,转换失败。
select * from GropBy where  [date] between '2017-07-25 00:00:00' and  '2017-07-25 24:00:00'  --从字符串转换日期和/或时间时,转换失败。

select * from GropBy where  [datetime] BETWEEN '2010-10' and '2015-10' --从字符串转换日期和/或时间时,转换失败。
select * from GropBy where  [datetime] between '2017-07-25 00:00:00' and  '2017-07-25 24:00:00'  --从字符串转换日期和/或时间时,转换失败。


select * from GropBy where  [date] BETWEEN convert(datetime,'30-03-2011',105) and convert(datetime,'30-03-2015',105) --从字符串转换日期和/或时间时,转换失败。

select * from GropBy where [date] between GETDATE() and  '2017-07-25 24:00:00' --时间超出格式 24:00 


use test
select * from GropBy  --数据库存储的是2010-10-01 00:00:00:00
select (convert(datetime,'30-03-2011',105)) ---把字符串转换为时间

select 当前日期=convert(varchar(100),getdate(),120)  --把时间转化为字符串 --varchar(10)--截取前10位



select * from GropBy where  [datetime] BETWEEN '2010-10' AND '2015-10'  --出错原因是时间格式不对2015-10
select * from GropBy where [datetime] BETWEEN '2017-07-25 00:00:00' and  '2017-07-25 24:00:00' --出错时间格式不正确 原因是24:00 超出时间范围
select * from GropBy where [datetime] BETWEEN '2010-05-01 00:00:00' and  '2017-07-25 23:59:59' --可以用
select * from GropBy where 1=1  and [datetime] BETWEEN '2010-05-01' AND '2015-06-15' --可以用
select * from GropBy where [datetime] BETWEEN '2010-05-01' AND '2015-06-15' --可以用
--查询 当天数据的方法

SELECT DATEDIFF(day, CONVERT(char(19), getdate(), 120), CONVERT(char(19), getdate(), 120)) AS DiffDate --比较时间, 相差0 select getdate() --获得当前时间 SELECT CONVERT(char(19), getdate(), 120) --格式化时间

select getdate()  --获得当前时间  2018-12-24 08:46:42.173
SELECT CONVERT(char(19), getdate(), 120) --格式化时间  2018-12-24 08:46:37
select convert(varchar(10), getdate(), 23)  --2018-12-24


select * from 扫码就诊 where convert(varchar(10), getdate(), 23)=convert(varchar(10), 就诊日期, 23) and 条码编号='信息' 

SELECT DATEDIFF(day, CONVERT(char(19), getdate(), 120), CONVERT(char(19), getdate(), 120)) AS DiffDate --比较时间, 相差0

select * from 扫码就诊 where datediff(dd,就诊日期,GETDATE())=0  and 条码编号='信息'   --判断datediff函数和当前时间和就诊时间比较 =0的时候
select (datediff(hh,starttime,endtime)-1)/24 + 1
from tb  -- 还能 运算...
原文地址:https://www.cnblogs.com/enych/p/9244575.html