您好,登錄后才能下訂單哦!
這篇文章主要講解了“PostgreSQL時(shí)間處理的常用方式有哪些”,文中的講解內(nèi)容簡(jiǎn)單清晰,易于學(xué)習(xí)與理解,下面請(qǐng)大家跟著小編的思路慢慢深入,一起來(lái)研究和學(xué)習(xí)“PostgreSQL時(shí)間處理的常用方式有哪些”吧!
now()函數(shù):
select now();
current_timestamp,同now():
select current_timestamp;
select current_time;
select current_date;
可以去掉now()、掉后面的+8等:
select now()::timestamp(0)without time zone; select current_timestamp::timestamp(0)without time zone;
語(yǔ)法:DATE_PART(field, source), filed可以理解為要截取的類型。
下面是filed支持的類型:
CENTURY,世紀(jì),獲取日期所在的世紀(jì):
select date_part('CENTURY', TIMESTAMP '2022-12-16 12:21:13'); select date_part('CENTURY', now());
MILLENNIUM,千年
select date_part('MILLENNIUM', timestamp '2022-12-16 13:21:15');
YEAR,年份域
select date_part('YEAR', timestamp '2022-12-16 13:21:15');
MONTH,對(duì)于timestamp數(shù)值,它是一年里的月份數(shù)(1-12);對(duì)于interval數(shù)值,它是月的數(shù)目,然后對(duì)12取模(0-11)
select date_part('MONTH', timestamp '2022-12-16 13:21:15');
select date_part('month', interval '2 years 5 months')
DAY,日期里的天,值是1-31:
select date_part('day', TIMESTAMP '2022-12-16 12:21:13'); select date_part('day', now());
HOUR,小時(shí)(0-23)
select date_part('HOUR', TIMESTAMP '2022-12-16 12:21:13');
MINUTE,分鐘域(0-59)
select date_part('MINUTE', TIME '2022-12-16 13:21:15');
SECOND,秒域,包括小數(shù)部分(0-59[1])
select date_part('SECOND', timestamp '2022-12-16 13:21:15');
MICROSECONDS,秒域(包括小數(shù))乘以 1,000,000
select date_part('MICROSECONDS', TIME '2022-12-16 13:21:15');
MILLISECONDS,秒域(包括小數(shù))乘以 1,000
select date_part('MILLISECONDS', timestamp '2022-12-16 13:21:15');
DECADE,年份域除以10:
select date_part('DECADE', TIMESTAMP '2022-12-16 12:21:13');
DOW,星期號(hào)(0-6;星期天是0) (僅用于timestamp)
select date_part('DOW', TIMESTAMP '2022-12-16 12:21:13'); select date_part('DOW', now());
DOY,一年中的第幾天(1 -365/366) (僅用于 timestamp)
select date_part('DOY', TIMESTAMP '2022-12-16 12:21:13');
QUARTER,該天所在的該年的季度(1-4)(僅用于 timestamp)
select date_part('QUARTER', timestamp '2022-12-16 13:21:15');
WEEK,該天在所在的年份里是第幾周。
select date_part('WEEK', timestamp '2022-12-16 13:21:15');
使用語(yǔ)法:extract (field from source),field 支持的類型,和date_part()函數(shù)一樣
select extract ('year' from timestamp '2022-12-16 13:21:15')
to_char(timestamp, text),把時(shí)間戳轉(zhuǎn)換成字串
select to_char(now(), 'YYYY-MM-DD HH24:MI:SS')
to_date(text, text) 把字串轉(zhuǎn)換成日期
select to_date('05 Dec 2022', 'DD Mon YYYY')
to_timestamp(text, text) ,把字串轉(zhuǎn)換成時(shí)間戳
select to_timestamp('05 Dec 2022', 'DD Mon YYYY')
select date '2001-09-28' + integer '7'; select date '2001-09-28' + interval '1 hour'; select date '2001-09-28' + time '03:00'; select interval '1 day' + interval '1 hour'; select timestamp '2001-09-28 01:00' + interval '23 hours'; select time '01:00' + interval '3 hours'; select - interval '23 hours'; select date '2001-10-01' - date '2001-09-28'; select date '2001-10-01' - integer '7'; select date '2001-09-28' - interval '1 hour'; select time '05:00' - time '03:00'; select time '05:00' - interval '2 hours; select timestamp '2001-09-28 23:00' - interval '23 hours'; select interval '1 day' - interval '1 hour'; select timestamp '2001-09-29 03:00' - timestamp '2001-09-27 12:00'; select interval '1 hour' * double precision '3.5'; select interval '1 hour' / double precision '1.5';
select now() + interval '10 min/year/month/day/hour/sec/ (1 year 1 month 1 day 1 hour 1 min 1 sec)' select now() - interval '10 min/year/month/day/hour/sec/ (1 year 1 month 1 day 1 hour 1 min 1 sec)' select now()::timestamp(0)without time zone-interval '72 hour' select extract(day from now() - '2001-09-27 12:00') from user ;
感謝各位的閱讀,以上就是“PostgreSQL時(shí)間處理的常用方式有哪些”的內(nèi)容了,經(jīng)過(guò)本文的學(xué)習(xí)后,相信大家對(duì)PostgreSQL時(shí)間處理的常用方式有哪些這一問(wèn)題有了更深刻的體會(huì),具體使用情況還需要大家實(shí)踐驗(yàn)證。這里是億速云,小編將為大家推送更多相關(guān)知識(shí)點(diǎn)的文章,歡迎關(guān)注!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。