选择日期字段在给定月份和年份中的行
问题描述:
我的SQL表如下:
id (int) | date (date) | text1 (varchar) | text2 (varchar)
我想选择日期适合给定月份和年份的行,而不管那天。
在选择语句中,月份和年份均以整数形式给出。
所以缺少的是哪里子句。也许 extract()
是我想要的东西,但是我不知道如何将它与两个整数一起使用,例如 2011
和 02
。
I want to select the lines whose date suits a given month and year, regardless of the day.
Both month and year are given in the select-statement as integers.
So the missing thing is the where-clause. Perhaps extract()
is the thing I'm looking for, but I don't know how to use it with the two integers, e.g. 2011
and 02
.
答
您可以使用提取:
SELECT * FROM yourtable
WHERE EXTRACT(month FROM "date") = 2
AND EXTRACT(year FROM "date") = 2011
但是情况下,您也可以这样做:
But in this case you could also do this:
SELECT * FROM yourtable
WHERE "date" >= '2011-02-01' AND "date" < '2011-03-01'