to_char()関数を使用せずにPostgreSQLの日付から年と月を抽出する方法は?


104

sql:を選択します SELECT "year-month" from table group by "year-month" AND order by date。年-月-日付の形式は「1978-01」、「1923-12」です。 couse workのto_charを選択しますが、「正しい」順序は選択しません。

to_char(timestamp_column, 'YYYY-MM')

1
to_charで順序が正しくないのはなぜですか?
yairchu 2013

1
to_char()が受け入れられない理由が明確でないため、不明確であるとして投票を終了します。
Alex R

回答:


67
date_part(text, timestamp)

例えば

date_part('month', timestamp '2001-02-16 20:38:40'),
date_part('year', timestamp '2001-02-16 20:38:40') 

http://www.postgresql.org/docs/8.0/interactive/functions-datetime.html


5
月と年を一度に抽出する方法?例を示すことができます
mokNathal

3
date_part( 'month'、timestamp '2001-02-16 20:38:40')、date_part( 'year'、timestamp '2001-02-16 20:38:40')
MK。

迅速な返信に感謝しますが、単一の関数でそれを行うことはできませんか、
それとも

2
あなたは何をしようとしているのですか?文字列を取得するだけですか?次に、必要な日付形式でTO_CHAR関数を使用するpostgresql.org/docs/8.2/static/functions-formatting.html
MKを。

181
to_char(timestamp, 'YYYY-MM')

順序は「正しい」とは言えないのですが、なぜ間違っているのかはわかりません(少なくとも10000年が来るまでは)。


「timestamp」を使用している場合to_char(to_timestamp(e。 "timestamp")、 'MM-YYYY')
Bruno Lee

@BrunoMarinho年代順に注文する場合は、「MM-YYYY for ordering」を使用しないでください。表示したい場合は、その形式の列を引き続き使用できますが、その順序で並べ
替える

4
なぜそれが受け入れられない答えなのか分かりません。
Prabowo Murti 2017年

この場合、ORDER BY日付を記入することはできません 。
aagjalpankaj

1
@Aviator:を使用できますORDER BY to_char(timestamp, 'YYYY-MM')。または、そうしたSELECT to_char(timestamp, 'YYYY-MM') AS date場合は、単純にを使用できますORDER BY date
yairchu 2018年

38

date_truncメソッドを使用して、日(または週、年、日など、その他の必要なもの)を切り捨てます。

注文からの売上を月ごとにグループ化する例:

select
  SUM(amount) as sales,
  date_trunc('month', created_at) as date
from orders
group by date
order by date DESC;

1
そのとおり。Uを使用して比較できます:date(date_trunc( 'month'、now()))= to_Date(5 :: varchar || '' || 2017 :: varchar、 'mm YYYY')
Alejandro Salamanca Mazuelo

「to_char(timestamp、 'YYYY-MM')」よりも2倍高速です。
Le Droid

20

次のコマンドを使用して、月の後にすべての情報を切り捨てることができますdate_trunc(text, timestamp)

select date_trunc('month',created_at)::date as date 
from orders 
order by date DESC;


例:

入力:

created_at = '2019-12-16 18:28:13'

出力1:

date_trunc('day',created_at)
// 2019-12-16 00:00:00

出力2:

date_trunc('day',created_at)::date 
// 2019-12-16

出力3:

date_trunc('month',created_at)::date 
// 2019-12-01

出力4:

date_trunc('year',created_at)::date 
// 2019-01-01

16

最初のオプション

date_trunc('month', timestamp_column)::date

すべての月が1日から始まる日付形式を維持します。

例:

2016-08-01
2016-09-01
2016-10-01
2016-11-01
2016-12-01
2017-01-01

2番目のオプション

to_char(timestamp_column, 'YYYY-MM')

@yairchuによって提案されたこのソリューションは、私の場合はうまくいきました。私は本当に「日」の情報を捨てたかったです。


11

EXTRACT関数pgSQLを使用できます

EX- date = 1981-05-31
EXTRACT(MONTH FROM date)
it will Give 05

詳細については、 PGSQL日時


1

「より小さい」ではなく「より大きい」関数で機能します。

例えば:

select date_part('year',txndt)
from "table_name"
where date_part('year',txndt) > '2000' limit 10;

正常に動作しています。

しかし

select date_part('year',txndt)
from "table_name"
where date_part('year',txndt) < '2000' limit 10;

エラーが発生します。


それは未満ではなくより大きい関数で機能しています。例:select date_part( 'year'、txndt)FROM "table_name" where date_part( 'year'、txndt)> '2000' limit 10; 正常に動作しています。ただし、select date_part( 'year'、txndt)FROM "hpi_validator_q3"。 "cdm_inv_exceptions"の場合、date_part( 'year'、txndt)<'2000'制限10; エラーが発生します。
Anurag Bhardwaj

1
これは回答ではありません。質問がある場合は、質問を回答として追加するのではなく、新しい質問を投稿してください。
Markoorn
弊社のサイトを使用することにより、あなたは弊社のクッキーポリシーおよびプライバシーポリシーを読み、理解したものとみなされます。
Licensed under cc by-sa 3.0 with attribution required.