์ƒˆ์†Œ์‹

๐Ÿ“šStudy/ETC

[PostgreSQL] Postgresql DIFF ๋‘ ๋‚ ์งœ ์‚ฌ์ด ๊ณ„์‚ฐ

  • -
SELECT DATE_PART('DAYS', CURRENT_TIMESTAMP AT TIME ZONE
                         'Asia/Seoul' - '2024-01-01 00:00:00'::TIMESTAMP)             AS DIFF,
       CURRENT_TIMESTAMP AT TIME ZONE 'Asia/Seoul' - '2023-01-01 00:00:00'::TIMESTAMP AS DIFF2;

 

DATE_PART

PostgreSQL์—์„œ๋Š” ํŠน์ • ๋‚ ์งœ ๋˜๋Š” ์‹œ๊ฐ„ ๊ฐ’์„ ์ถ”์ถœํ•  ์ˆ˜ ์žˆ๋Š” ํ•จ์ˆ˜๊ฐ€ ์กด์žฌํ•œ๋‹ค. 

์‚ฌ์šฉ ๊ตฌ๋ฌธ:

DATE_PART(field, source)

 

ํ•ด๋‹น ํ•จ์ˆ˜์˜ source ๋ถ€๋ถ„์— ๋‘ ๋‚ ์งœ์˜ ์ฐจ์ด๋ฅผ ๊ตฌํ•˜๋Š” ์ˆ˜์‹์„ ๋„ฃ๊ฒŒ ๋˜๋ฉด, days, hours, mins, secs ์ˆœ์œผ๋กœ ๊ณ„์‚ฐํ•ด์„œ ๋ณด์—ฌ์ค€๋‹ค.

(years์™€ mons ๋„ ์กด์žฌํ•˜๋‚˜ days๊ฐ€ ์ตœ๋Œ€๋กœ ๋ณด์ธ๋‹ค.)

๋‘ ๋‚ ์งœ์˜ ์ฐจ์ด๋ฅผ ๊ตฌํ•˜๋Š” ๊ฒฝ์šฐ, ์ตœ๋Œ€ ์ˆ˜์น˜๋Š” DAY๋กœ ํ™˜์‚ฐ๋˜๋Š”๋ฐ, ์ด๋ ‡๊ฒŒ ๊ณ„์‚ฐ๋œ ๊ฐ’์„ DATE_PART ํ•จ์ˆ˜์˜ 'DAY' ๋ฅผ ์ด์šฉํ•ด ์ผ์ž๋งŒ ์ถ”์ถœํ•˜๋Š” ๊ฒƒ์ด๋‹ค.

 

*DATE_PART ์˜ field ์—์„œ ์ง€์›ํ•˜๋Š” ํ˜•์‹ ๋ชฉ๋ก์„ ๊ฐ„๋žตํ•˜๊ฒŒ ์†Œ๊ฐœ

  • century
  • decade
  • year
  • month
  • day
  • hour
  • minute
  • second
  • microseconds
  • milliseconds
  • dow
  • doy
  • epoch
  • isodow
  • isoyear
  • timezone
  • timezone_hour
  • timezone_minute

์˜ˆ์‹œ

์‚ฌ์šฉ ๋ฐฉ๋ฒ•์€ ์•„๋ž˜์™€ ๊ฐ™๋‹ค.

 

์ฟผ๋ฆฌ

SELECT CURRENT_TIMESTAMP AT TIME ZONE
       'Asia/Seoul'                                                                   AS TODAY,
       '2024-01-01 00:00:00'::TIMESTAMP                                               AS DIFFDATE,
       DATE_PART('DAY', CURRENT_TIMESTAMP AT TIME ZONE
                        'Asia/Seoul' - '2024-01-01 00:00:00'::TIMESTAMP)              AS DIFF_V1,
       CURRENT_TIMESTAMP AT TIME ZONE 'Asia/Seoul' - '2024-01-01 00:00:00'::TIMESTAMP AS DIFF_V2

 

์ฟผ๋ฆฌ ์‹คํ–‰๊ฒฐ๊ณผ

 


์ฐธ๊ณ 

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

๋ฐ˜์‘ํ˜•
Contents

ํฌ์ŠคํŒ… ์ฃผ์†Œ๋ฅผ ๋ณต์‚ฌํ–ˆ์Šต๋‹ˆ๋‹ค

์ด ๊ธ€์ด ๋„์›€์ด ๋˜์—ˆ๋‹ค๋ฉด ๊ณต๊ฐ ๋ถ€ํƒ๋“œ๋ฆฝ๋‹ˆ๋‹ค.