sql 时间模糊查询

数据库中的表leve , 字段为 id , add_date
add_date的格式为:
id add_date
1 2012-10-24 7:00:00
2 2012-10-24
3 2012-10-25 7:00:00
4 2012-12-24 7:00:00

假如我要查询所有2012年10月的所有数据怎么写SQL语句?
应该是查处id 1 , 2, 3
3条数据

不是oracle 是PostgreSQL

第1个回答  2012-12-17
查询12年10份的所有数据:
SELECT * FROM leve WHERE datediff(month,add_date,‘2012-10-01’)=0追问

不对。
column "month" does not exist

追答

SELECT * FROM leve WHERE datediff(month,add_date,'2012-10-01')=0

这里你得看到一个问题,复制到SQL执行页面时,注意单引号。如果你那边还报错的话,请问你的是SQL Server数据库吗?我这边运行是正常的。

追问

sorry是PostgreSQL不是oracle

第2个回答  2012-12-17
select * from where convert(nvarchar(6),add_date,112)='201210'
如果是1月份,就是201201追问

不对
function nvarchar(integer) does not exist

追答

什麼情况?你是什麼环境啊?

追问

不是oracle 是PostgreSQL

追答

哦...我这只适用SQL SERVER

第3个回答  2012-12-17
Select * from employee where left (CONVERT(varchar(100), hire_date, 23),7)='2012-10'追问

这些23,7是什么???

追答

CONVERT(varchar(100), hire_date, 23) 查询 葛时 2006-05-16
left (,7)是取左边起7位数

oracle
select * from leve where to_date(add_date ,'YYYY-Mm')='2012-10'

追问

sorry不是oracle
是PostgreSQL

相似回答