Date_trunc ua dab tsi hauv SQL?
Date_trunc ua dab tsi hauv SQL?

Video: Date_trunc ua dab tsi hauv SQL?

Video: Date_trunc ua dab tsi hauv SQL?
Video: Kev Muag Khoom thiab Kev Sib Cuam Tshuam Nrog Cov Neeg Qhua Yam Muaj Kev Nyab Xeeb 2024, Kaum ib hlis
Anonim

Cov date_trunc muaj nuj nqi truncates lub TIMESTAMP los yog ib tug INTERVAL tus nqi raws li ib tug teev hnub ib feem, piv txwv li, teev, lis piam, los yog hli thiab xa rov qab cov truncated timestamp los yog luv luv nrog ib theem ntawm precision.

Yog li ntawd, Datetrunc yog dab tsi?

DATETRUNC (date_part, date, [start_of_week]) Txiav cov hnub teev rau qhov tseeb uas tau teev tseg los ntawm date_part. Txoj haujlwm no rov qab hnub tshiab. Piv txwv li, thaum koj txiav ib hnub uas nyob rau nruab nrab ntawm lub hli ntawm lub hli theem, qhov haujlwm no rov qab thawj hnub ntawm lub hli.

Tom qab ntawd, lo lus nug yog, Kuv yuav ua li cas thiaj li tau txais qhov sib txawv ntawm lub sijhawm hauv PostgreSQL?

  1. Yog tias koj xav tau cov txiaj ntsig hauv teev, hli, hnub, teev, thiab lwm yam: SELECT hnub nyoog (timestamp1, timestamp2);
  2. Yog tias koj xav tau cov txiaj ntsig hauv vib nas this xwb: SELECT EXTRACT(EPOCH NTAWM timestamp 'timestamp1') - EXTRACT(EPOCH FROM timestamp 'timestamp2');
  3. Los yog casting nws li no:

Ib yam li ib tug yuav nug, dab tsi yog lub sijhawm hauv SQL?

Qhov < lub sijhawm > hais txog lub sij hawm nce los ntsuas ntawm ob hnub. Piv txwv li, txhawm rau txiav txim siab tus nqi feem ntawm cov teev lossis hnub ntawm hnub pib thiab hnub kawg. Cov nqi siv tau yog thib ob, feeb, teev, hnub, thiab hli.

Postgres Sysdate yog dab tsi?

SYSDATE yog Oracle nkaus xwb. Tus qauv ANSI txhais cov hnub tam sim no lossis tam sim no_timestamp uas tau txais kev txhawb nqa los ntawm Postgres thiab sau rau hauv phau ntawv: postgresql ua .org/docs/current/static/functions-datetime.html#FUNCTIONS-DATETIME-CURRENT. (Btw: Oracle txhawb CURRENT_TIMESTAMP ib yam)

Pom zoo: