問題原由:
intouch項目中,利用intouch腳本來存儲數據時,存入的時間格式為:date,time分開存儲。在報表需求中,有需要利用查詢兩個時間段之間的數據。
問題解決:
1.直接寫腳本(寫出的腳本有bug)
表結構如下:
select * from 在線數據日報表
where(convert(char(10),date,120)>='2018-10-30' and time>='18:00:00')
and (convert(char(10),date,120)<='2018-11-1' and time<='23:00:00')
bug產生的原因:time也是關鍵字,我想去10.30到11.1之間所有的時間點,因為time位於18:00:00和23:00:00,所以取出來的數為10.30和11.1兩天內,18點到23點的所有數據。不符合設想。
2.利用視圖整合
沒有其他辦法,只能將date列和time列整合成為datetime列,然后再進行sql篩選。
2.1新建視圖
SELECT CONVERT(varchar(30), date) + ' ' + CONVERT(varchar(30), time) AS datetime1, id, CSLJ
FROM dbo.在線數據日報表
3.效果測試
select * from 水量計算
where (convert(char(30),datetime1,120)>='2018-10-30 18:00') and (convert(char(30),datetime1,120)<='2018-11-1 23:00')
GO
測試基本達到效果,實現兩個時間段之間的數據查找。