[MySQL] 이전 달 및 날짜 계산.
SELECT NOW(), DAY(NOW()), MONTH(NOW()), YEAR(NOW()), HOUR(NOW()), MINUTE(NOW()), SECOND(NOW())
-- 2024-02-21 19:14:59 21 2 2024 19 14 59
> SELECT NOW(), DATE_ADD(NOW(), INTERVAL (DAY(NOW()) * -1) DAY)
-- 2024-02-21 19:05:31 2024-01-31 19:05:31
> SELECT NOW(), DATE_ADD(NOW(), INTERVAL (DAY(NOW()) * -1) + 1 DAY)
-- 2024-02-21 19:10:28 2024-02-01 19:10:28
> SELECT NOW(), DATE_ADD(DATE_ADD(NOW(), INTERVAL (DAY(NOW()) * -1) + 1 DAY), INTERVAL -1 MONTH)
-- 2024-02-21 19:12:02 2024-01-01 19:12:02
SELECT NOW(),
DATE_FORMAT(NOW(),'%Y%m%d'),
DATE_FORMAT(DATE_FORMAT(NOW(),'%Y%m%d'),'%Y-%m-%d %H:%i:%s'),
CAST(DATE_FORMAT(NOW(),'%Y%m%d') AS DATETIME)
-- 2024-02-21 19:28:09 20240221 2024-02-21 00:00:00 2024-02-21 00:00:00
> SELECT NOW(),
DATE_ADD(CAST(DATE_FORMAT(NOW(),'%Y%m%d') AS DATETIME), INTERVAL (DAY(NOW()) * -1) DAY)
-- 2024-02-21 19:31:33 2024-01-31 00:00:00
> SELECT NOW(),
CAST(DATE_FORMAT(NOW(),'%Y%m01') AS DATETIME),
DATE_ADD(CAST(DATE_FORMAT('2024-01-31 00:00:00','%Y%m01') AS DATETIME), INTERVAL 1 MONTH)
-- 2024-02-26 14:17:24 2024-02-01 00:00:00 2024-03-01 00:00:00
※ 티스토리 띠워쓰기 부분이 syntax 에러 발생하니 띠워쓰기 수정하고 조회 할 것.