常州市做省煤器厂家:这个SQL,该怎么写............

来源:百度文库 编辑:查人人中国名人网 时间:2024/04/29 02:25:25
问一下,一个数据库中有几个字段。其中一个字段的值是用的now()来添加的...例如:2006-7-20 19:08:33
我想写一个sql语句.查询所有.字段日期为 !当天时间! 的记录
比如有这么三条记录
id time message
1 2006-7-20 19:08:33 123
2 2006-8-1 19:08:45 123234
3 2006-8-1 19:28:45 213
如果是7月20查.能查到第一条...如果是8月1查...那么我想通过查询后.直接得到后两条记录...
望高手帮忙!

如果是在SQL查询分析器中你可以:
Sql Server 2000中的语句为
select * from tablename where time=getdate()
如果在C#中你可以
"select * from tablename where time="+DateTime.Now()
在vbscript中:SELECT *
FROM 表1
where datediff('d', [time], now()) = 0

Sql Server 2000中的语句为
select * from tablename where time=getdate()

SELECT *
FROM 表1
where datediff('d', [time], now()) = 0

select * from 表名
where time like "2006-8-1%"

因为Sql的时间精确到了毫秒,所以查询时不能直接用纯日期去匹配。可以这么想: 先假设这个时间是现在的时间2006-8-15 11:14:23:000,如果给它加上一整天(可用函数),那么肯定大于2006-8-15;如果给它减去一天(可用函数),那么肯定小于2006-8-15;反过来如果符合这两个条件,那么这个时间就在今天。

比如我查询SQL自带pubs数据库title表里2006年8月6日 的记录
use pubs
select * from titles where (dateadd(day,1,pubdate)>='20000806') and (dateadd(day,-1,pubdate)<='20000806')

我QQ 694025239