欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

PostgreSql日期類型處理詳細實例

 更新時間:2023年05月16日 11:19:14   作者:白衣無暇  
PostgreSQL提供了大量用于獲取系統(tǒng)當前日期和時間的函數,例如 current_date、current_time、current_timestamp、clock_timestamp()等,這篇文章主要給大家介紹了關于PostgreSql日期類型處理的相關資料,需要的朋友可以參考下

1. 查詢天數據

查詢當天數據

select * from table1 as n
where n.created_time>=current_date;

查詢昨天數據

select * from table1 as n
where n.created_time>=current_date-1 and n.created_time <current_date ;

2. 查詢月數據

查詢當月數據

select *
from table1 as n
WHERE extract(YEAR FROM created_time) = extract(YEAR FROM now())
and extract(MONTH FROM created_time) = extract(MONTH FROM now()) 

查詢上月數據

select *
from table1 as n
where created_time >= date_trunc('month',current_date - interval '1' month)
and created_time < date_trunc('month',current_date)

3. 查詢年數據

查詢當年數據

select *
from table1 as n
WHERE extract(YEAR FROM created_time) = extract(YEAR FROM now()) ORDER BY created_time

查詢去年數據

select *
from table1 as n
where created_time >= date_trunc('year',current_date - interval '1' year)
and created_time < date_trunc('year',current_date)

4.類型轉換

1.查詢某天:datetime類型的,需要轉換為 date 類型,如果你要查詢的字段已經是 date 類型則不需要進行轉換

select t_create
from table
where t_create::date = to_date(‘2023-02-08', ‘YYYY-MM-DD');

2.string轉timestamp類型,按范圍查詢

select * from table where create_date >= ‘2023-01-08'::timestamp and create_date < ‘2023-02-08'::timestamp;

3.時間戳Long轉Timestamp

select TO_TIMESTAMP(1512490630)

4.string轉data,只能得到年月日,得不到時分秒

select to_date(‘2023-01-28 12:55:05')

5.當前日期 select current_date

6.帶時區(qū)的時分秒值 select current_time;也可以使用current_time(precision),將結果在四分之一秒的范圍內四舍五入到位數,比如select current_time(2);對應沒有時區(qū)的值:select localtime;

7.帶時區(qū)的年月日時分秒值 select current_timestamp; 對應沒有時區(qū)的值:select localtimestamp;

補充:時區(qū)轉換

有些時候,時區(qū)轉換對于特定時間在不同時區(qū)顯示特別有用。AT TIME ZONE提供了這種功能,它是如何做到的?我們將在一個事務中進行演示,因為同一事務中now()函數總是返回相同的值,從而我們可以很容易看到同一時間在不同時區(qū)顯示的差別。

postgres=# BEGIN;
BEGIN
postgres=# SELECT now();
       now
-------------------------------
 2013-08-26 12:39:39.122218+02
postgres=# SELECT now() AT TIME ZONE 'GMT';
     timezone
----------------------------
 2013-08-26 10:39:39.122218
postgres=# SELECT now() AT TIME ZONE 'GMT+1';
     timezone
----------------------------
 2013-08-26 09:39:39.122218
postgres=# SELECT now() AT TIME ZONE 'PST';
     timezone
----------------------------
 2013-08-26 02:39:39.122218

總結

到此這篇關于PostgreSql日期類型處理的文章就介紹到這了,更多相關PostgreSql日期類型處理內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

最新評論