spring 스프링/iBatis 아이바티스

날짜 검색 쿼리 - 월별, 년도별 1 (오라클, iBatis)

KIMSG 2017. 7. 28. 10:58

이번에 날짜별로 검색을 하는 쿼리를 짜면서 제약조건이 정말 중요하다는 것을 알았다.


날짜별로 검색할 때 고려해야 할 것


1. 년도

2. 월

3. 날짜

4. 시간


년도 별로 검색하기

 (테이블의 데이터 컬럼의 정의에 따라 달라질 수 있음)  DATE 형태라고 가정..



2016년 ~ 2017년

=>>   201601010000 ~~ 201712310000


로 검색을 해야한다.    


'YYYYMMDDHH24MI'


은 현재 날짜의 형태를 어떻게 출력하겠냐는 것이다. 년월일시분 까지 한다는 의미이다.




1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<!-- 검색조건 부여 -->
<sql id="sms_status">
      <isNotEmpty property="search_year">
            <isNotEmpty property="search_month">
                  <![CDATA[AND A.REGDATE BETWEEN to_date( #search_year#||#search_month#||'010000''YYYYMMDDHH24MI' )
                          AND to_date( (SELECT TO_CHAR(LAST_DAY(#search_year#||#search_month#||'01'), 'YYYYMMDD'FROM DUAL), 'YYYYMMDDHH24MI' )+0.99999]]>
            </isNotEmpty>
            <isEmpty property="search_month">
                  <![CDATA[AND A.REGDATE BETWEEN to_date( #search_year#||'01010000''YYYYMMDDHH24MI' )
                               AND to_date( #search_year#||'12310000''YYYYMMDDHH24MI' )+0.99999]]>
            </isEmpty>
      </isNotEmpty>
</sql>
cs




1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<select name="search_year" id="search_year">
      <option value="" <c:if test="${searchVO.search_year == ''}">selected="selected"</c:if> >전체년도</option>
      <c:forEach begin="0" end="15" varStatus="status">
       <option value="${nowYear - status.current}" <c:if test="${searchVO.search_year == nowYear - status.current}">selected="selected"</c:if> >${nowYear - status.current}년</option>
   </c:forEach>
</select>
<select name="search_month">
      <option value="">전체월</option>
      <c:forEach begin="1" end="12" varStatus="status">
            <c:if test="${status.current < 10}">
                  <option value="0${status.current}" <c:if test="${searchVO.search_month == status.current}">selected="selected"</c:if> >${status.current}월</option>
            </c:if>
            <c:if test="${status.current >= 10}">
            <option value="${status.current}" <c:if test="${searchVO.search_month == status.current}">selected="selected"</c:if> >${status.current}월</option>
            </c:if>
  </c:forEach>
</select>
cs