안녕하세요 반갑습니다.
미리 감사드립니다! 아래 형변환 질문 한번 검토 부탁드립니다.
날짜 데이터가 들어가는 컬럼에 정보의 성격이 TIMESTAMP 가정 >> 컬럼명 mdate
저는 보통 오라클에서 작업을 해서, 아래와 같이 그냥 to_char로 바꿔서 기간을 잡고 조회를 합니다.
mysql은 형변환이 CAST와 CONVERT 두개인데 기능은 같고, 사용 방식만 다른것으로 알고있습니다.
아래와같이 convert로 date형으로 변경해서, between '2021-08-01' and '2021-08-10'로 결과값은 만들었는데
timestamp 날짜 시간 컬럼을 더 편하게 기간을 조회할 방법은 어떻게 할 수 있나요?
예시)
- 21년 8월 1~10일까지 데이터를 추출
[ORACLE]
Select *
from test_tb
where to_char(mdate, 'yyyymmdd') between '20210801' and '20210810';
[Mysql] - 21년 8월 1~10일까지 데이터를 추출 >> 더 간단히 형변환하여 날짜 기간을 조회하고 싶습니다.
select convert(mdate, date)
from test_tb
where convert(mdate, date) between '2021-08-01' and '2021-08-10';
mariadb이긴 합니다만..date나 timestamp라면 형변환 없이도 되는 것 같네요.
MariaDB [test]> create table t1 (a timestamp); Query OK, 0 rows affected (0.005 sec) MariaDB [test]> insert into t1 values ('2021-08-04'); Query OK, 1 row affected (0.001 sec) MariaDB [test]> insert into t1 values ('2021-08-03'); Query OK, 1 row affected (0.001 sec) MariaDB [test]> insert into t1 values ('2021-08-02'); Query OK, 1 row affected (0.001 sec) MariaDB [test]> select * from t1 where a >='2021-08-04'; +---------------------+ | a | +---------------------+ | 2021-08-04 00:00:00 | +---------------------+ 1 row in set (0.001 sec) MariaDB [test]> create table t2 (a date); Query OK, 0 rows affected (0.004 sec) MariaDB [test]> insert into t2 values ('2021-08-02'); Query OK, 1 row affected (0.001 sec) MariaDB [test]> insert into t2 values ('2021-08-03'); Query OK, 1 row affected (0.001 sec) MariaDB [test]> select * from t2 where a >='2021-08-04'; Empty set (0.000 sec) MariaDB [test]> select * from t2 where a >='2021-08-02'; +------------+ | a | +------------+ | 2021-08-02 | | 2021-08-03 | +------------+ 2 rows in set (0.001 sec)