»
»
Contents
基本形
to_charSELECT n :: date, to_char(n, 'Month'), -- 月名, 先頭大文字, 9文字パディング to_char(n, 'Mon'), -- 短縮月名, 先頭大文字, 全て3文字 to_char(n, 'Day'), -- 曜日名, 先頭大文字, 9文字パディング to_char(n, 'Dy') -- 短縮曜日名, 先頭大文字, 3文字 FROM now() AS n; n | to_char | to_char | to_char | to_char ------------+-----------+---------+-----------+--------- 2015-07-19 | July | Jul | Sunday | Sun

上の結果のとおり、月の短縮形
» メール通信講座 英語
全て大文字 / 小文字にする場合
検索等の利便を考えて、曜日・月名を全て大文字SELECT n :: date, to_char(n, 'MONTH, MON, DAY, DY') -- 全て大文字 FROM now() AS n; n | to_char ------------+-------------------------------- 2015-07-19 | JULY , JUL, SUNDAY , SUN SELECT n :: date, to_char(n, 'month, mon, day, dy') -- 全て小文字 FROM now() AS n; n | to_char ------------+-------------------------------- 2015-07-19 | july , jul, sunday , sun

空白を除きたければ、書式の先頭に FM を付ける
上の結果のように、非短縮形の月・曜日名は、常にSELECT n :: date, '---' || to_char(n, 'FMMonth---FMDay') || '---' FROM now() AS n; n | ?column? ------------+--------------------- 2015-07-19 | ---July---Sunday---

英語の月・曜日名の一覧を出すクエリ
いろんな方法があると思いますが、今日は連番を出すSELECT *, to_char(concat, 'Month Mon') FROM generate_series(1, 12) AS month, cast(concat('2015-', month, '-1') AS date); month | concat | to_char -------+------------+--------------- 1 | 2015-01-01 | January Jan 2 | 2015-02-01 | February Feb 3 | 2015-03-01 | March Mar 4 | 2015-04-01 | April Apr 5 | 2015-05-01 | May May 6 | 2015-06-01 | June Jun 7 | 2015-07-01 | July Jul 8 | 2015-08-01 | August Aug 9 | 2015-09-01 | September Sep 10 | 2015-10-01 | October Oct 11 | 2015-11-01 | November Nov 12 | 2015-12-01 | December Dec (12 行)

SELECT extract(dow FROM concat) AS dow, extract(isodow FROM concat) AS isodow, to_char(concat, 'Day Dy') FROM generate_series(1, 7) AS day, cast(concat('2015-1-', day) AS date) ORDER BY 1; dow | isodow | to_char -----+--------+--------------- 0 | 7 | Sunday Sun 1 | 1 | Monday Mon 2 | 2 | Tuesday Tue 3 | 3 | Wednesday Wed 4 | 4 | Thursday Thu 5 | 5 | Friday Fri 6 | 6 | Saturday Sat (7 行)

逆に、月名を月番号に変換するクエリ
何の役に立つか分かりませんが、最後に。月名から各月SELECT month, extract(month FROM concat) FROM unnest( -- 下に、変換したい月名をカンマ区切りで自由に入力 '{ January, Feb, March, Apr, May, Jun, Jul, August, Sep, Sept, Oct, November, Dec }' :: text[]) AS t(month), cast(concat('1 ', month, ' 2015') AS date); month | date_part ----------+----------- January | 1 Feb | 2 March | 3 Apr | 4 May | 5 Jun | 6 Jul | 7 August | 8 Sep | 9 Sept | 9 Oct | 10 November | 11 Dec | 12 (13 行)
