postgresql是如何求年龄的_Postgresql 通过出生日期获取年龄的操作
三個基礎(chǔ)的時間表示函數(shù) current_date/current_time/now()
select current_date ;
返回當前日期以 年-月-日(yyyy-mm-dd)的形式:
2019-01-10
select current_time;
返回當日時間以 時:分:秒+時區(qū)(hh:mm:ss )的形式:
17:49:11.585308+08
select now();
返回當前時間 以 年-月-日 時:分:秒(yyyy-mm-dd hh:mm:ss)的形式:
2019-01-10 17:50:45.354983+0
然后幾個計算公式
第一種是直接對日期進行操作,
select (current_date - '1993-01-01')/365 age; select (current_date - '1993-01-01 16:00:00')/365 age;
輸出 26 26 兩種時間格式結(jié)果一致
第二種方式
select age(current_date, '1993-01-01') age; select age(current_date, '1993-01-01 16:00:00') age;
輸出
26 years 9 days
26 years 8 days 08:00:00
age()函數(shù)會輸出精確的年齡字符串,根據(jù)傳入的時間不同,輸出的時間格式相對應(yīng)
如果使用now()函數(shù)計算,需要注意age()函數(shù)能輸出正確結(jié)果,直接計算單位為day
select age(current_date, '1993-01-01') age;
select age(current_date, '1993-01-01 16:00:00') age;
select (now() - '1993-01-01')/365 age;
select (now() - '1993-01-01 16:00:00')/365 age
分別輸出
26 years 9 days 19:20:46.395713
26 years 9 days 03:20:46.421816
26 days 01:02:21.497111
26 days 00:59:43.688958
補充:postgresql獲取當前時間的四種方式
postgresql中有四種獲取當前時間的方式。
1.now()
返回值:當前年月日、時分秒,且秒保留6位小數(shù)。
2.current_timestamp
返回值:當前年月日、時分秒,且秒保留6位小數(shù)。(同上)
申明:now和current_timestamp幾乎沒區(qū)別,返回值相同,建議用now。
3.current_time
返回值:時分秒,秒最高精確到6位
4.current_date
返回值:年月日
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持萬仟網(wǎng)。如有錯誤或未考慮完全的地方,望不吝賜教。
希望與廣大網(wǎng)友互動??
點此進行留言吧!
創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎勵來咯,堅持創(chuàng)作打卡瓜分現(xiàn)金大獎總結(jié)
以上是生活随笔為你收集整理的postgresql是如何求年龄的_Postgresql 通过出生日期获取年龄的操作的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 中控ecs700 mysql_浙大中控E
- 下一篇: java word文档生成目录_JAVA