본문 바로가기
Database/PostgreSQL

[PostgreSQL] DATE_PART 사용하기

by pcm9881 2023. 4. 13.

DATE_PART는 날짜 또는 시간 값에서 하위 필드를 추출하는 함수입니다.

 

추출

 

1.  연

SELECT DATE_PART('year', TIMESTAMP '2023-04-13'); -- 2023

 

 

2.  월

SELECT DATE_PART('month', TIMESTAMP '2023-04-13'); -- 4

 

 

3.  일

SELECT DATE_PART('day', TIMESTAMP '2023-04-13'); -- 13

 

 

4.  시분초

SELECT date_part('hour',TIMESTAMP '2023-04-18 12:34:56') h,
       date_part('minute',TIMESTAMP '2023-04-18 12:34:56') m,
       date_part('second',TIMESTAMP '2023-04-18 12:34:56') s;
      
-- 12, 34, 56

 

계산

 

1.  연수 계산

SELECT DATE_PART('year', TIMESTAMP '2023-04-13') - DATE_PART('year', TIMESTAMP '2021-04-13'); -- 2

 

 

2.  일수 계산

SELECT DATE_PART('day', '2023-04-13'::timestamp - '2023-04-12'::timestamp); -- 1

 

참조

[postgresql tutorial date_part]: https://www.postgresqltutorial.com/postgresql-date-functions/postgresql-date_part/

 

PostgreSQL DATE_PART Function: Extract Year, Month, Week, etc., From a Timestamp

Introduction to the PostgreSQL DATE_PART function Summary: in this tutorial, we will introduce you to the PostgreSQL DATE_PART() function that allows you to retrieve subfields e.g., year, month, week from a date or time value. The DATE_PART() function extr

www.postgresqltutorial.com

 

728x90

댓글