DATE_TRUNC()
[ 설명 ]
- 특정 날짜를 연, 월, 주, 일 단위로 "절단(Truncate)"하는 함수
- 주어진 날짜의 해당 단위 시작일을 반환
- 결과는 TIMESTAMP 타입
- DATE_TRUNC()는 특정 기간(주, 월, 연도)의 시작점을 구할 때 유용!
[ 기본 문법 ]
DATE_TRUNC('단위', date_column)
단위
- year 해당 연도의 시작일 2025-01-01 00:00:00.000
- month 해당 월의 시작일 2025-03-01 00:00:00.000
- week 해당 주의 시작일 (월요일) 2025-03-10 00:00:00.000
- day 해당 날짜의 시작시간 2025-03-17 00:00:00.000
- hour 해당 시간의 시작시간 2025-03-17 14:00:00.000
[ 예제 ]
SELECT
DATE_TRUNC('year', current_date) AS year_start, -- 연도 시작 (2025-01-01)
DATE_TRUNC('month', current_date) AS month_start, -- 월 시작 (2025-03-01)
DATE_TRUNC('week', current_date) AS week_start, -- 주 시작 (2025-03-10)
DATE_TRUNC('day', current_date) AS day_start; -- 날짜 시작 (2025-03-17)
DATE_PARSE()
[ 설명 ]
- 문자열(VARCHAR)을 DATE 또는 TIMESTAMP로 변환
- 문자열이 특정 형식(YYYYMMDD, YYYY-MM-DD 등)으로 저장되어 있을 때 사용
- 출력 결과는 TIMESTAMP 타입
[ 기본 문법 ]
DATE_PARSE('날짜문자열', '형식')
입력 문자열 형식 지정 (%Y%m%d 등)
- '20250317' '%Y%m%d' = 2025-03-17 00:00:00.000
- '2025-03-17' '%Y-%m-%d' = 2025-03-17 00:00:00.000
- '17-03-2025' '%d-%m-%Y' = 2025-03-17 00:00:00.000
[ 예제 ]
SELECT
DATE_PARSE('20250317', '%Y%m%d') AS parsed_date1, -- 2025-03-17 00:00:00.000
DATE_PARSE('2025-03-17', '%Y-%m-%d') AS parsed_date2, -- 2025-03-17 00:00:00.000
DATE_PARSE('17-03-2025', '%d-%m-%Y') AS parsed_date3; -- 2025-03-17 00:00:00.000
DATE_ADD()
[ 설명 ]
- 날짜에 특정 기간을 더하거나 빼는 함수
- 단위는 year, month, week, day, hour, minute, second 가능
- 출력 결과는 DATE 또는 TIMESTAMP
[ 기본 문법 ]
DATE_ADD('단위', 추가할 값, date_column)
- 추가할 값이 양수(+) → 미래 날짜
- 추가할 값이 음수(-) → 과거 날짜
함수 결과 (2025-03-17 기준)
- DATE_ADD('day', 7, current_date) 2025-03-24
- DATE_ADD('week', -2, current_date) 2025-03-03
- DATE_ADD('month', 3, current_date) 2025-06-17
- DATE_ADD('year', -1, current_date) 2024-03-17
[ 예제 ]
SELECT
DATE_ADD('day', 7, current_date) AS plus_7_days, -- 7일 후
DATE_ADD('week', -2, current_date) AS minus_2_weeks, -- 2주 전
DATE_ADD('month', 3, current_date) AS plus_3_months, -- 3개월 후
DATE_ADD('year', -1, current_date) AS minus_1_year; -- 1년 전
current_date, current_timestamp
[ 기본 문법 ]
함수 설명 예제 결과 (2025-03-17 기준)
- current_date 현재 날짜 = (DATE) 2025-03-17
- current_timestamp 현재 날짜 및 시간 = (TIMESTAMP) 2025-03-17 14:23:45.678
[ 예제 ]
SELECT current_date, current_timestamp;
EXTRACT()
- 날짜에서 특정 요소(연도, 월, 일, 시간 등)만 추출
[ 예제 ]
SELECT
EXTRACT(YEAR FROM current_date) AS year_part, -- 연도 (2025)
EXTRACT(MONTH FROM current_date) AS month_part, -- 월 (3)
EXTRACT(DAY FROM current_date) AS day_part, -- 일 (17)
EXTRACT(WEEK FROM current_date) AS week_part; -- 연중 몇 번째 주인지 (11)
FORMAT_DATETIME()
- 날짜를 특정 형식의 문자열로 변환
- 날짜를 문자열(VARCHAR)로 변환할 때 유용
[ 예제 ]
SELECT FORMAT_DATETIME('%Y-%m-%d', current_timestamp) AS formatted_date;
'Platform > AWS' 카테고리의 다른 글
AWS - DynamoDB GSI (0) | 2025.04.11 |
---|---|
AWS - DynamoDB Pagination과 Where절 (0) | 2025.03.23 |
AWS - REST API 설정 가이드 (0) | 2024.09.22 |
AWS - Lambda VPC를 사용하여 고정 IP 주소 생성 (0) | 2024.08.28 |
AWS - Lambda Runtime 실행환경 (0) | 2024.08.28 |