[SQL] 시간 관련 쿼리 SQL2015. 12. 28. 14:57
1. 현재 시간에서 10초를 빼기 위한 방법
select getdate(), getdate() - (CAST(1 AS FLOAT) / 24 / 60 / 60 * 10)
2. 현재 시간에서 10초를 더하기
select getdate(), convert(varchar(30), dateadd(s, 10, getdate()), 121)
3. 조회 조건이 날짜와 시간이 아니라 특정 시간에 대한 내용 찾기
A. 조건을 시간 단위로 찾기
select * from Termination_Call_Detail where datename(hh, DateTime) between '10' and '11'
또는
select * from Termination_Call_Detail where datepart(hh, DateTime) between '10' and '11'
select * from Termination_Call_Detail where datename(mi, DateTime) between '00' and '30'
또는
select * from Termination_Call_Detail where datepart(mi, DateTime) between '00' and '30'
select * from Termination_Call_Detail
where datename(WEEKDAY, DateTime) = '월요일' and datename(HOUR, DateTime) between '10' and '11'
또는
select * from Termination_Call_Detail
where datepart(WEEKDAY, DateTime) = '2' and datename(HOUR, DateTime) between '10' and '11'
-- WEEKDAY 결과 1:일,2:월,3:화,4:수,5:목,6:금,7:토
[출처] MSSQL 시간으로 쿼리 하기|작성자 욘들