develog

[MySql] 날짜 연산 본문

DB/MySQL

[MySql] 날짜 연산

냐옴 2013. 11. 28. 18:11
SELECT
    NOW() AS DATE현재,
    DATE_ADD(NOW(), INTERVAL -1 DAY) AS DATE현재_1일전,
    DATE_ADD(NOW(), INTERVAL -1 WEEK) AS DATE현재_일주일전,
    DATE_ADD(NOW(), INTERVAL -1 MONTH) AS DATE현재_1달전,
    DATE_ADD(NOW(), INTERVAL -1 YEAR) AS DATE현재_1년전
;

SELECT
    DATE_FORMAT(NOW(), '%Y-%m-%d') AS DATE현재,
    DATE_ADD(DATE_FORMAT(NOW(), '%Y-%m-%d'), INTERVAL -1 DAY) AS DATE현재_1일전,
    DATE_ADD(DATE_FORMAT(NOW(), '%Y-%m-%d'), INTERVAL -1 WEEK) AS DATE현재_일주일전,
    DATE_ADD(DATE_FORMAT(NOW(), '%Y-%m-%d'), INTERVAL -1 MONTH) AS DATE현재_1달전,
    DATE_ADD(DATE_FORMAT(NOW(), '%Y-%m-%d'), INTERVAL -1 YEAR) AS DATE현재_1년전
;

SELECT
    STR_TO_DATE('2023-02-13', '%Y-%m-%d') AS DATE기준일,
    DATE_ADD(STR_TO_DATE('2023-02-13', '%Y-%m-%d'), INTERVAL -1 DAY) AS DATE기준일_1일전,
    DATE_ADD(STR_TO_DATE('2023-02-13', '%Y-%m-%d'), INTERVAL -1 WEEK) AS DATE기준일_일주일전,
    DATE_ADD(STR_TO_DATE('2023-02-13', '%Y-%m-%d'), INTERVAL -1 MONTH) AS DATE기준일_1달전,
    DATE_ADD(STR_TO_DATE('2023-02-13', '%Y-%m-%d'), INTERVAL -1 YEAR) AS DATE기준일_1년전
;

SELECT
    DATE_FORMAT(NOW(), '%Y-%m-%d %H:%i:%s'),
    STR_TO_DATE('2023-02-13', '%Y-%m-%d'),
    DATE_FORMAT(STR_TO_DATE('2023-02-13', '%Y-%m-%d'), '%Y-%m-%d %H:%i:%s')
;

 

'DB > MySQL' 카테고리의 다른 글

[MySQL] 함수 만들기  (0) 2013.12.01
[MySQL] 프로시저 소스, information_schema.ROUTINES  (0) 2013.12.01
HeidiSQL  (0) 2013.03.07
당월, 전월, 익월  (0) 2013.03.06
[MySql] 날짜로 조회  (0) 2013.03.06
Comments