select * from table1 where datediff(day,ldate,'2005-01-01')=0
如果要用like: select * from table1 where convert(varchar,ldate,120) like '2005-01-01%'因为你保存在数据库里的日期格式不是'2005-01-01'这种格式,所以先要用convert转换
like是一个字符串函数,字段ldate是日期型,楼主认为呢?1、将ldate转为'YYYY-MM-DD HH:MI:SS'格式的字符串再用like比较select * from table1 where convert(varchar(25),ldate,120) like '2005-01-01%'2、直接用日期时间函数datediff对日期型数据进行比较select * from table1 where datediff(dd,ldate,'2005-01-01')=0 注:函数会隐含自动转换'2005-01-01'为datetime类型数据
sqlserver日期型不是可以提供隐是转换char类型么?
to operfume(橘子香水) :like只能用在字符型字段。这个说法是错误的
--要求是查询2005,1,1这一天的数据 select * from table1 where convert(varchar(10),ldate,120)='2005-01-01'
where datediff(day,ldate,'2005-01-01')=0
select * from table1 where convert(varchar,ldate,120) like '2005-01-01%'因为你保存在数据库里的日期格式不是'2005-01-01'这种格式,所以先要用convert转换
注:函数会隐含自动转换'2005-01-01'为datetime类型数据
select * from table1 where convert(varchar(10),ldate,120)='2005-01-01'
'2005-01-01%'--字符型