柚子快報(bào)邀請碼778899分享:數(shù)據(jù)庫 【PGSQL】date
柚子快報(bào)邀請碼778899分享:數(shù)據(jù)庫 【PGSQL】date
date_trunc 函數(shù)用于在 PostgreSQL 中將日期或時(shí)間戳值截?cái)啵ㄏ蛳氯≌┑街付ǖ木燃墑e。當(dāng)您想要忽略較小的時(shí)間單位(例如,小時(shí)、分鐘、秒),專注于較大的單位(例如,天、月、年)時(shí),該函數(shù)非常有用。date_trunc 的語法如下:
date_trunc(unit, source);
unit:指定要將源值截?cái)嗟降臅r(shí)間單位??梢允且韵轮唬?/p>
'microseconds'(微秒)'milliseconds'(毫秒)'second' 或 'seconds'(秒)'minute' 或 'minutes'(分鐘)'hour' 或 'hours'(小時(shí))'day' 或 'days'(天)'week' 或 'weeks'(周)'month' 或 'months'(月)'quarter' 或 'quarters'(季度)'year' 或 'years'(年)
通過指定 unit,您可以將 source 的值截?cái)嗟剿璧臅r(shí)間精度。以下是一些示例:
-- 將時(shí)間戳截?cái)嗟椒昼?/p>
SELECT date_trunc('minute', current_timestamp);
-- 2024-01-17 08:08:00
--
-- 將時(shí)間戳截?cái)嗟叫r(shí)
SELECT date_trunc('hour', current_timestamp);
-- 2024-01-17 08:00:00
--
-- 將時(shí)間戳截?cái)嗟教?/p>
SELECT date_trunc('day', current_timestamp);
-- 2024-01-17 00:00:00
--
-- 將時(shí)間戳截?cái)嗟皆?/p>
SELECT date_trunc('month', TIMESTAMP '2024-02-16 14:32:45');
-- 2024-02-01 00:00:00
--
-- 將時(shí)間戳截?cái)嗟侥?/p>
SELECT date_trunc('year', TIMESTAMP '2024-02-16 14:32:45');
-- 2024-01-01 00:00:00
--
-- 一天開始
SELECT date_trunc('day', current_timestamp);
--
-- 一天結(jié)束
SELECT date_trunc('day', current_timestamp) + INTERVAL '1 day' - interval '1 second';
這些查詢將返回截?cái)嗟街付▎挝坏娜掌诨驎r(shí)間戳。
柚子快報(bào)邀請碼778899分享:數(shù)據(jù)庫 【PGSQL】date
推薦文章
本文內(nèi)容根據(jù)網(wǎng)絡(luò)資料整理,出于傳遞更多信息之目的,不代表金鑰匙跨境贊同其觀點(diǎn)和立場。
轉(zhuǎn)載請注明,如有侵權(quán),聯(lián)系刪除。