sql语句如何查日期字段的某天的数据?

比如有个日期字段的值为"2010-9-23 10:10:10"
我如果直接select * from [table] where [date] = '2010-9-23'的话
是查不出的
要如何才能像这样只输日期就查出来?

1、创建测试表,

create table test_date(id varchar2(20), v_date date);

2、插入测试数据

insert into test_date values(1, to_date('2010-9-23 10:10:10','yyyy-mm-dd hh24:mi:ss'));

insert into test_date values(2, to_date('2010-9-24 10:10:10','yyyy-mm-dd hh24:mi:ss'));

insert into test_date values(3, to_date('2010-9-25 10:10:10','yyyy-mm-dd hh24:mi:ss'));

insert into test_date values(4, to_date('2010-9-26 10:10:10','yyyy-mm-dd hh24:mi:ss'));

insert into test_date values(5, to_date('2010-9-27 10:10:10','yyyy-mm-dd hh24:mi:ss'));

commit;


3、查询表中全量数据,select t.*, rowid from test_date t;

4、编写sql,查询日期为2010-9-23的数据;

   select t.* from test_date t where to_char(v_date,'yyyymmdd') = 20100923;

温馨提示:答案为网友推荐,仅供参考
第1个回答  推荐于2018-03-12
不知道你讲的是那种数据库

1、Sqlserver 2000
select * from [table] where convert(varchar(10),[date],120) = '2010-09-23'

2、SqlServer 2005
估计跟2000是一样的 具体我也没试验了

3、Orcale
select * from [table] where [date] = to_date ('2010-9-23','yyyy-mm-dd')

除了2005 其他都测试过 可以查出来的本回答被网友采纳
第2个回答  2010-09-25
SELECT * FROM [TABLE]
WHERE CONVERT(VARCHAR(10),[date])='2010-9-23'
这样就可以把所有2010-9-23的记录查出来了
第3个回答  2010-09-25
select * from [table] where convert(varchar(10),[date],120) = '2010-09-23'

注意,后边是2010-09-23本回答被提问者采纳
第4个回答  2010-09-25
如果是Oracle的话,使用这个语句。
select * from [table] where trunc([date]) = to_date('2010-9-23','YYYY-MM-DD')