Select * From Table Where DataDiff(d,getdate(),日期字段)=0
Select * From Table Where convert(varchar(10),日期字段,120)=日期值
不知道你要用来做什么. 类型转换: create table t1(a datetime) insert into t1 select '2007-08-06 08:41:25.000' select left(convert(varchar,a,120),10) from t1如果是时间段比较 between and 注意如果是比较2007-8-1 到2007-8-6 要写成 between '2007-8-1' and '2007-8-7' 要dateadd 1天 程序会把 between '2007-8-1' and '2007-8-7' 看做 between '2007-8-1 00:00:00.000' and '2007-8-7 00:00:00.000'
select * from 表 where convert(varchar(10),日期,126)='2007-08-06'
你需要进行一下转换,'2007-08-06'--这样写会被默认成字符串,你可以定义一个datetime类型的变量@m_time,然后给这个变量赋值,再进行引用:declare @m_time datetime set @m_time ='2007-08-06' select col1,col2.... from tablename where time=@m_time; 或者你把要查询的日期条件进行一次转换: select col1,col2.... from tablename where time=conver(datetime,''2007-08-06') 或者 select col1,col2.... from tablename where convert(varchar(20),time,102)='2007-08-06'
Sorry, select col1,col2.... from tablename where convert(varchar(20),time,102)='2007-08-06' 应该改为: select col1,col2.... from tablename where convert(varchar(10),time,120)='2007-08-06'
类型转换:
create table t1(a datetime)
insert into t1 select '2007-08-06 08:41:25.000'
select left(convert(varchar,a,120),10) from t1如果是时间段比较
between and
注意如果是比较2007-8-1 到2007-8-6
要写成 between '2007-8-1' and '2007-8-7'
要dateadd 1天
程序会把 between '2007-8-1' and '2007-8-7'
看做 between '2007-8-1 00:00:00.000' and '2007-8-7 00:00:00.000'
set @m_time ='2007-08-06' select col1,col2.... from tablename where time=@m_time;
或者你把要查询的日期条件进行一次转换:
select col1,col2.... from tablename where time=conver(datetime,''2007-08-06') 或者
select col1,col2.... from tablename where convert(varchar(20),time,102)='2007-08-06'
select col1,col2.... from tablename where convert(varchar(20),time,102)='2007-08-06'
应该改为:
select col1,col2.... from tablename where convert(varchar(10),time,120)='2007-08-06'