查詢要求:查詢日期在每日07:30到10:00之間的所有日期的資料。
create table exam (
opdt datetime,
sm varchar(20))go
with ex (opdt,sm) as
( select '2019-02-01 07:29:59','不符' union all
select '2019-02-01 07:30:00','符合' union all
select '2019-02-01 07:30:01','符合' union all
select '2019-02-01 09:59:59','符合' union all
select '2019-02-01 10:00:00','符合' union all
select '2019-02-01 10:00:01','不符' union all
select '2019-02-02 07:29:59','不符' union all
select '2019-02-02 07:30:00','符合' union all
select '2019-02-02 07:30:01','符合' union all
select '2019-02-02 09:59:59','符合' union all
select '2019-02-02 10:00:00','符合' union all
select '2019-02-02 10:00:01','不符' union all
select '2019-12-01 07:29:59','不符' union all
select '2019-12-01 07:30:00','符合' union all
select '2019-12-01 07:30:01','符合' union all
select '2019-12-01 09:59:59','符合' union all
select '2019-12-01 10:00:00','符合' union all
select '2019-12-01 10:00:01','不符' union all
select '2019-12-02 07:29:59','不符' union all
select '2019-12-02 07:30:00','符合' union all
select '2019-12-02 07:30:01','符合' union all
select '2019-12-02 09:59:59','符合' union all
select '2019-12-02 10:00:00','符合' union all
select '2019-12-02 10:00:01','不符'
( select '2019-02-01 07:29:59','不符' union all
select '2019-02-01 07:30:00','符合' union all
select '2019-02-01 07:30:01','符合' union all
select '2019-02-01 09:59:59','符合' union all
select '2019-02-01 10:00:00','符合' union all
select '2019-02-01 10:00:01','不符' union all
select '2019-02-02 07:29:59','不符' union all
select '2019-02-02 07:30:00','符合' union all
select '2019-02-02 07:30:01','符合' union all
select '2019-02-02 09:59:59','符合' union all
select '2019-02-02 10:00:00','符合' union all
select '2019-02-02 10:00:01','不符' union all
select '2019-12-01 07:29:59','不符' union all
select '2019-12-01 07:30:00','符合' union all
select '2019-12-01 07:30:01','符合' union all
select '2019-12-01 09:59:59','符合' union all
select '2019-12-01 10:00:00','符合' union all
select '2019-12-01 10:00:01','不符' union all
select '2019-12-02 07:29:59','不符' union all
select '2019-12-02 07:30:00','符合' union all
select '2019-12-02 07:30:01','符合' union all
select '2019-12-02 09:59:59','符合' union all
select '2019-12-02 10:00:00','符合' union all
select '2019-12-02 10:00:01','不符'
)select *
from ex
where opdt>='2019-01-01' and opdt<='2019-12-31'
and datepart(hh,opdt)*10000+datepart(mi,opdt)*100+datepart(ss,opdt) >= 073000
and datepart(hh,opdt)*10000+datepart(mi,opdt)*100+datepart(ss,opdt) <= 100000
with ex (opdt,sm) as
( select '2019-02-01 07:29:59','不符' union all
select '2019-02-01 07:30:00','符合' union all
select '2019-02-01 07:30:01','符合' union all
select '2019-02-01 09:59:59','符合' union all
select '2019-02-01 10:00:00','符合' union all
select '2019-02-01 10:00:01','不符' union all
select '2019-02-02 07:29:59','不符' union all
select '2019-02-02 07:30:00','符合' union all
select '2019-02-02 07:30:01','符合' union all
select '2019-02-02 09:59:59','符合' union all
select '2019-02-02 10:00:00','符合' union all
select '2019-02-02 10:00:01','不符' union all
select '2019-12-01 07:29:59','不符' union all
select '2019-12-01 07:30:00','符合' union all
select '2019-12-01 07:30:01','符合' union all
select '2019-12-01 09:59:59','符合' union all
select '2019-12-01 10:00:00','符合' union all
select '2019-12-01 10:00:01','不符' union all
select '2019-12-02 07:29:59','不符' union all
select '2019-12-02 07:30:00','符合' union all
select '2019-12-02 07:30:01','符合' union all
select '2019-12-02 09:59:59','符合' union all
select '2019-12-02 10:00:00','符合' union all
select '2019-12-02 10:00:01','不符'
)select *
from ex
where opdt>='2019-01-01' and opdt<='2019-12-31'
and substring(convert(varchar(20),opdt,108),12,8)>='07:30:00'
and substring(convert(varchar(20),opdt,108),12,8)<='10:00:00'
查詢指定時間段的資料
一 select from searchdetails where searchdatetime between 2014 10 06 01 40 50.123 and 2014 12 07 23 12 and datepart hour,searchdatetime between 1 and 1...
sql2005自動刪除指定時間段的記錄
利用sql2005資料庫提供的作業來完成該功能,步驟如下 1.啟動sqlserver 服務,不啟動則作業不會執行。2.右擊 作業 新建作業 3.在 新建作業 對話方塊中,輸入名稱。選擇類別。4.選擇項 步驟。單擊 新建 按鈕。5.在 新建作業步驟 對話方塊中,輸入步驟名稱,型別選擇 transact...
Sql 按照指定天數時間段查詢
begin try declare begintime datetime 2017 02 01 declare endtime datetime 2017 03 3 23 59 59 declare showtype int 0 展示的型別 0 日 1 十日 2 月 declare cuscount...