[SQL]搜尋一個日期區間中的一個時段(時間)的指令
最近案子要計算電費,所以要從資料庫中搜尋某一段日期的用電量來計算,但是台電又有區分早上和晚上用電電費不同。所以在此先紀錄SQL command,可以搜尋某一段日期區間中的時間區間。
SQL COMMAND
SELECT [Time_Stamp] ,[Power_BCPM_KW_PDU_A_01] ,[Power_BCPM_KW_PDU_A_02] ,[Power_BCPM_KW_PDU_A_03] FROM [dbo].[historyData_BCPM_KW] WHERE (Time_Stamp BETWEEN '2014/1/1' AND '2014/2/1') AND (DATEPART(hh,Time_Stamp) BETWEEN 14 AND 16)
補充追加(星期搜尋)
SELECT [Time_Stamp] ,[Power_BCPM_KW_PDU_A_01] ,[Power_BCPM_KW_PDU_A_02] ,[Power_BCPM_KW_PDU_A_03] FROM [dbo].[historyData_BCPM_KW] WHERE (Time_Stamp BETWEEN '2014/1/1' AND '2014/2/1') AND (DATEPART(hh,Time_Stamp) BETWEEN 14 AND 16) and (DATEPART(WEEKDAY,Time_Stamp)= 7) --1:星期日,2:星期一,3:星期二...7:星期六
留言
張貼留言