postsql时间计算
生活随笔
收集整理的這篇文章主要介紹了
postsql时间计算
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
一、獲取系統時間函數
1.1 獲取當前完整時間
select now(); select now(); now ------------------------------- 2013-04-12 15:39:40.399711+08 (1 row) current_timestamp 同 now() 函數等效。select current_timestamp; now ------------------------------- 2013-04-12 15:40:22.398709+08 (1 row)1.2 獲取當前日期
select current_date; select current_date; date ------------ 2013-04-12 (1 row)1.3 獲取當前時間
select current_time; select current_time; timetz -------------------- 15:43:31.101726+08 (1 row)二、時間的計算
select now(); now ------------------------------- 2013-04-12 15:47:13.244721+08 (1 row)2.1 兩年后
select now() + interval '2 years';?column? ------------------------------- 2015-04-12 15:49:03.168851+08 (1 row) select now() + interval '2 year';?column? ------------------------------- 2015-04-12 15:49:12.378727+08 (1 row) select now() + interval '2 y'; ?column? ------------------------------ 2015-04-12 15:49:25.46986+08 (1 row)select now() + interval '2 Y'; ?column? ------------------------------- 2015-04-12 15:49:28.410853+08 (1 row) select now() + interval '2Y';?column? ------------------------------- 2015-04-12 15:49:31.122831+08 (1 row)2.2 一個月后
select now() + interval '1 month';?column? ------------------------------ 2013-05-12 15:51:22.24373+08 (1 row) select now() + interval 'one month'; ERROR: invalid input syntax for type interval: "one month" LINE 1: select now() + interval 'one month';2.3 三周前
select now() - interval '3 week'; ?column? ------------------------------- 2013-03-22 16:00:04.203735+08 (1 row)2.4 十分鐘后
select now() + '10 min'; ?column? ------------------------------- 2013-04-12 16:12:47.445744+08 (1 row) 說明: interval 可以不寫,其值可以是: Abbreviation Meaning Y Years M Months (in the date part) W Weeks D Days H Hours M Minutes (in the time part) S Seconds2.5 計算兩個時間差
使用 age(timestamp, timestamp) david=# select age(now(), timestamp '1989-02-05'); age ---------------------------------------- 24 years 2 mons 7 days 17:05:49.119848 (1 row) select age(timestamp '2007-09-15'); age ------------------------ 5 years 6 mons 27 days (1 row)三、時間字段的截取
在開發過程中,經常要取日期的年,月,日,小時等值,PostgreSQL 提供一個非常便利的EXTRACT函數。
field 表示取的時間對象,source 表示取的日期來源,類型為 timestamp、time 或 interval。
3.1 取年份
3.2 取月份
select extract(month from now()); date_part ----------- 4 (1 row) select extract(day from timestamp '2013-04-13'); date_part ----------- 13 (1 row) SELECT EXTRACT(DAY FROM INTERVAL '40 days 1 minute'); date_part ----------- 40 (1 row)3.3 查看今天是一年中的第幾天
select extract(doy from now()); date_part ----------- 102 (1 row)3.4 查看現在距1970-01-01 00:00:00 UTC 的秒數
select extract(epoch from now()); date_part ------------------ 1365755907.94474 (1 row)3.5?把epoch 值轉換回時間戳
SELECT TIMESTAMP WITH TIME ZONE 'epoch' + 1369755555 * INTERVAL '1 second'; ?column? ------------------------ 2013-05-28 23:39:15+08 (1 row) 因為要做一個where條件?adate varchar(12) 201610101010?
要篩選 adate > 系統時間 的數據。
你的adate的格式是否為年月日時分(示例就變成2016-10-10 10:10),可以用to_timestamp轉換為日期時間值: to_timestamp(adate, 'YYYYMMDDHH24MI') 不過,從你的需求來看,adate可能為數據表的字段,要提高執行效率的化,應該是將系統時間轉換為字串來進行比較,而不是將字段值轉換為日期時間值來比較。因為將字段轉換為日期時間值后比較的化,肯定不能使用索引了(除非你定義了轉換后的索引),而且每次查詢都有左轉換效率太差了。故這樣來做篩選條件會比較好些: where adate > to_char(current_timestamp, 'YYYYMMDDHH24MI') <![CDATA[and o.create_time >= to_date(#{beginDate},'YYYY-MM-DD')]]><![CDATA[and o.create_time <= (to_date(#{endDate},'YYYY-MM-DD')+1)]]> replace(o.isbn,'-','') = #{search_value} lower(str)總結
以上是生活随笔為你收集整理的postsql时间计算的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【mybatis】mybatis多表联查
- 下一篇: 一些PHP函数功能