PostgreSQL拒绝仅显示月份和年份

时间:2016-11-02 12:42:11

标签: sql postgresql date format

这是原始表

"facultyid","courseid","datequalified"  
2143,"ISM 3112","9/1988"  
2143,"ISM 3113","9/1988"  
3467,"ISM 4212","9/1995"  
3467,"ISM 4930","9/1996"  
4756,"ISM 3112","9/1991"  
4756,"ISM 3113","9/1991"  

CREATE TABLE qualified (
    facultyid integer NOT NULL,
    courseid character varying(16) NOT NULL,
    datequalified character varying(16),
    dq2 date
);


ALTER TABLE qualified OWNER TO postgres;

--
-- TOC entry 2136 (class 0 OID 16441)
-- Dependencies: 190
-- Data for Name: qualified; Type: TABLE DATA; Schema: class_scheduling_01; Owner: postgres
--

INSERT INTO qualified (facultyid, courseid, datequalified, dq2) VALUES (3467, 'ISM 4212', '9/1995', '1995-09-01');
INSERT INTO qualified (facultyid, courseid, datequalified, dq2) VALUES (4756, 'ISM 3112', '9/1991', '1991-09-01');
INSERT INTO qualified (facultyid, courseid, datequalified, dq2) VALUES (2143, 'ISM 3113', '9/1988', '1988-09-01');
INSERT INTO qualified (facultyid, courseid, datequalified, dq2) VALUES (4756, 'ISM 3113', '9/1991', '1991-09-01');
INSERT INTO qualified (facultyid, courseid, datequalified, dq2) VALUES (2143, 'ISM 3112', '9/1988', '1988-09-01');
INSERT INTO qualified (facultyid, courseid, datequalified, dq2) VALUES (3467, 'ISM 4930', '9/1996', '1996-09-01');

我不知道如何存储该值,只有月份和年份,所以我决定使用varchar类型。从那里我决定是否要执行日期查询命令,我必须将它从字符串转换为日期。我想要执行的一个问题是找出哪个教师ID与1991年之后的日期相关联。

我使用以下to_date函数将字符串转换为日期: UPDATE限定SET dq2 = to_date(日期限定,' MM \ YYYY')

输出dq2列如下:

dq2
1995-09-01
1991-09-01
1988-09-01
1991-09-01
1988-09-01
1996-09-01

是否可以将dq2输出作为月份和年份,并且仍然允许与日期相关的查询?

1 个答案:

答案 0 :(得分:1)

SELECT to_char(your_date, 'YYYY-MM') AS month_format
FROM YourTable