날짜 포맷 함수
날짜 포맷 함수 (Date Format Functions)
strftime과 strptime 함수는 [DATE]({% link docs/current/sql/data_types/date.md %}) / [TIMESTAMP]({% link docs/current/sql/data_types/timestamp.md %}) 값과 문자열 사이를 변환하는 데 쓸 수 있어요. CSV 파일을 파싱하거나, 사용자에게 출력을 보여주거나, 프로그램 간에 정보를 전달할 때 자주 필요하죠. 날짜 표현 방식이 정말 다양하기 때문에 이 함수들은 날짜·타임스탬프가 어떻게 구조화되어야 하는지를 설명하는 포맷 문자열을 받아요.
출처: 문서
본문
strftime 예시
[strftime(timestamp, format)]({% link docs/current/sql/functions/timestamp.md %}#strftimetimestamp-format)은 지정된 패턴에 따라 타임스탬프나 날짜를 문자열로 변환해요.
SELECT strftime(DATE '1992-03-02', '%d/%m/%Y');
02/03/1992
SELECT strftime(TIMESTAMP '1992-03-02 20:32:45', '%A, %-d %B %Y - %I:%M:%S %p');
Monday, 2 March 1992 - 08:32:45 PM
strptime 예시
[strptime(text, format) 함수]({% link docs/current/sql/functions/timestamp.md %}#strptimetext-format)는 지정된 패턴에 따라 문자열을 타임스탬프로 변환해요.
SELECT strptime('02/03/1992', '%d/%m/%Y');
1992-03-02 00:00:00
SELECT strptime('Monday, 2 March 1992 - 08:32:45 PM', '%A, %-d %B %Y - %I:%M:%S %p');
1992-03-02 20:32:45
strptime 함수는 실패하면 오류를 던져요.
SELECT strptime('02/50/1992', '%d/%m/%Y') AS x;
Invalid Input Error:
Could not parse string "02/50/1992" according to format specifier "%d/%m/%Y"
02/50/1992
^
Error: Month out of range, expected a value between 1 and 12
실패 시 NULL을 반환받고 싶다면 [try_strptime 함수]({% link docs/current/sql/functions/timestamp.md %}#try_strptimetext-format)를 사용해요.
NULL
CSV 파싱
날짜 포맷은 CSV 파싱 중에도 지정할 수 있어요. [COPY 문]({% link docs/current/sql/statements/copy.md %})이나 read_csv 함수에서 말이죠. DATEFORMAT이나 TIMESTAMPFORMAT(또는 둘 다)을 지정하면 돼요. DATEFORMAT은 날짜 변환에, TIMESTAMPFORMAT은 타임스탬프 변환에 사용돼요. 사용 방법 예시를 몇 가지 볼게요.
COPY 문에서:
COPY dates FROM 'test.csv' (DATEFORMAT '%d/%m/%Y', TIMESTAMPFORMAT '%A, %-d %B %Y - %I:%M:%S %p');
read_csv 함수에서:
SELECT *
FROM read_csv('test.csv', dateformat = '%m/%d/%Y', timestampformat = '%A, %-d %B %Y - %I:%M:%S %p');
포맷 지정자 (Format Specifiers)
아래는 사용 가능한 모든 포맷 지정자의 전체 목록이에요.
| Specifier | Description | Example |
|---|---|---|
%a |
Abbreviated weekday name. | Sun, Mon, ... |
%A |
Full weekday name. | Sunday, Monday, ... |
%b |
Abbreviated month name. | Jan, Feb, ..., Dec |
%B |
Full month name. | January, February, ... |
%c |
ISO date and time representation | 1992-03-02 10:30:20 |
%d |
Day of the month as a zero-padded decimal. | 01, 02, ..., 31 |
%-d |
Day of the month as a decimal number. | 1, 2, ..., 30 |
%f |
Microsecond as a decimal number, zero-padded on the left. | 000000 - 999999 |
%g |
Millisecond as a decimal number, zero-padded on the left. | 000 - 999 |
%G |
ISO 8601 year with century representing the year that contains the greater part of the ISO week (see %V). |
0001, 0002, ..., 2013, 2014, ..., 9998, 9999 |
%H |
Hour (24-hour clock) as a zero-padded decimal number. | 00, 01, ..., 23 |
%-H |
Hour (24-hour clock) as a decimal number. | 0, 1, ..., 23 |
%I |
Hour (12-hour clock) as a zero-padded decimal number. | 01, 02, ..., 12 |
%-I |
Hour (12-hour clock) as a decimal number. | 1, 2, ... 12 |
%j |
Day of the year as a zero-padded decimal number. | 001, 002, ..., 366 |
%-j |
Day of the year as a decimal number. | 1, 2, ..., 366 |
%m |
Month as a zero-padded decimal number. | 01, 02, ..., 12 |
%-m |
Month as a decimal number. | 1, 2, ..., 12 |
%M |
Minute as a zero-padded decimal number. | 00, 01, ..., 59 |
%-M |
Minute as a decimal number. | 0, 1, ..., 59 |
%n |
Nanosecond as a decimal number, zero-padded on the left. | 000000000 - 999999999 |
%p |
Locale's AM or PM. | AM, PM |
%S |
Second as a zero-padded decimal number. | 00, 01, ..., 59 |
%-S |
Second as a decimal number. | 0, 1, ..., 59 |
%u |
ISO 8601 weekday as a decimal number where 1 is Monday. | 1, 2, ..., 7 |
%U |
Week number of the year. Week 01 starts on the first Sunday of the year, so there can be week 00. Note that this is not compliant with the week date standard in ISO 8601. | 00, 01, ..., 53 |
%V |
ISO 8601 week as a decimal number with Monday as the first day of the week. Week 01 is the week containing Jan 4. Note that %V is incompatible with year directive %Y. Use the ISO year %G instead. |
01, ..., 53 |
%w |
Weekday as a decimal number. | 0, 1, ..., 6 |
%W |
Week number of the year. Week 01 starts on the first Monday of the year, so there can be week 00. Note that this is not compliant with the week date standard in ISO 8601. | 00, 01, ..., 53 |
%x |
ISO date representation | 1992-03-02 |
%X |
ISO time representation | 10:30:20 |
%y |
Year without century as a zero-padded decimal number. Numbers 00 to 68 are turned into 2000 to 2068. Numbers 69 to 99 are turned into 1969 to 1999. | 00, 01, ..., 99 |
%-y |
Year without century as a decimal number. Numbers 0 to 68 are turned into 2000 to 2068. Numbers 69 to 99 are turned into 1969 to 1999. | 0, 1, ..., 99 |
%Y |
Year with century as a decimal number. | 2013, 2019 etc. |
%z |
Time offset from UTC in the form ±HH:MM, ±HHMM, or ±HH. | -0700 |
%Z |
Time zone name. | Europe/Amsterdam |
%% |
A literal % character. |
% |