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 에러 발생하니 띠워쓰기 수정하고 조회 할 것.
'[ Programing ] > Database' 카테고리의 다른 글
[MySQL] 현재 날짜의 첫날과 다음달의 첫날 구하기 (0) | 2024.03.13 |
---|---|
[SQL] CASE WHEN (0) | 2024.02.27 |
[MySQL] Lock 발생시 모니터링. (0) | 2024.02.20 |
[DB] 프로시저에서 SP 호출. (0) | 2023.11.22 |
[MySQL] View Table 삭제&생성 (0) | 2023.11.16 |