2016-06-27 3 views
0

으로 캡처합니다. 클라이언트 ID, 이름 및 서비스 날짜와 같은 필드를 선택합니다. 내 where 절에 매일 매일 쿼리를 실행하면 현재 날짜의 첫 번째 날짜를 현재 날짜로 캡처합니다.현재 월의 현재 날짜의 첫 번째 날짜 범위를

+0

http://stackoverflow.com/questions/1520789/how-can-i-select-the-first-day-of-a-month-in-sql 나머지는 쉬워야합니다. 힌트'DATEADD (월, DATEDIFF (월, 0, @ 일), 0)와 @ mydate 사이의 날짜 –

답변

2

한 가지 방법은 월과 연도를 비교하는 것입니다 :

where year(col) = year(getdate()) and 
     month(col) = month(getdate()) and 
     day(col) <= day(getdate()) -- this is optional, if there is no future data 

또 다른 방법은 달 초에 비교하는 것입니다. (이 테이블의 미래 데이터가없는 가정합니다.)

where col >= cast(dateadd(day, 1 - day(getdate), getdate()) as date) 

을하거나, 더 나은 아직 : 나는대로이 접근 할

where col >= datefromparts(year(getdate()), month(getdate()), 1) 
0
Select * 
    From YourTable 
    where SomeDateField bewteen cast(DateAdd(DD,-Day(GetDate())+1,GetDate()) as Date) and cast(getDate() as Date) 
     and ... 

편집 : I 미래의 날짜/예정된 이벤트를 잡기 위해 BETWEEN을 사용했습니다.

관련 문제