select * from news where adddate>='2005-6-16' and adddate <='2005-6-17'在默认情况下等于select * from news where adddate>='2005-6-16 00:00:00' and adddate <='2005-6-17 00:00:00'帮助里这么写:如果只指定日期,则时间默认为 12:00 AM(午夜)。
如果日期字段adddate为DateTime型,查询的时候最好用DateDiff来判断。
可以这样select * from news where adddate>='2005-6-16' and adddate <='2005-6-17 23:59:59'或select * from news where adddate>='2005-6-16' and adddate <'2005-6-18'如果你的adddate密度小于秒 就用第二种
我解释一下:1.sql查询语句为:select * from news where adddate>='2005-6-16' and adddate <='2005-6-17'时, 返回的结果只有2005-6-16的记录,却没有2005-6-17的记录;你这个记录的产生是这样的,你的2006-06-17的那条记录,是不是含有时:分:秒数值呀, 而且一定是大于‘2006-06-07 00:00:00’以后的,是不是??实际上系统默认你上面的查询语句,是这样的 select * from news where adddate>='2005-6-16 00:00:00' and adddate <='2005-6-17 00:00:00'如果你有记录是在2005-06-07 00:00:00 以后的,假设你有2001-06-07 01:00:00 这样的记录, 当然也就查询不出来啦 我想以下那两条语句,是同样的道理。
谢谢啦,那ACCESS数据库是不是也一样?
1.sql查询语句为:select * from news where adddate>='2005-6-16' and adddate <='2005-6-17'时,返回的结果只有2005-6-16的记录,却没有2005-6-17的记录; select * from news where convert(char(10),adddate,120)>='2005-06-16' and convert(char(10),adddate,120) <='2005-06-17' 2.sql查询语句为:select * from news where adddate>='2005-6-16'时,返回的结果正常,有2005-6-16的记录也有2005-6-17和以后的记录; 3.sql查询语句为:select * from news where adddate>='2005-6-16' and adddate <='2005-6-16'时,返回的结果没有记录!關於這些問題都可以用類型轉換函數convert()解決
1: select * from news where convert(char(10),adddate,120)>='2005-06-16' and convert(char(10),adddate,120) <='2005-06-17'
convert成字符串,再比较大小,好不好?跟比较时间的大小,那个效率高?
DateTime类型是“yyyy-MM-dd hh:mm:ss.ms”是由年、月、日、时、分、秒、毫秒组成。你用adddate>='2005-6-16' and adddate <='2005-6-16'时将自动变成:adddate>='2005-6-16 00:00:00.000' and adddate <='2005-6-16 00:00:00.000'你用系统时间加入数据库时很难有'2005-6-16 00:00:00.000'时刻加入的所以只得到adddate>'2005-6-16 00:00:00.000' and adddate <'2005-6-16 00:00:00.000'
1.sql查询语句为:select * from news where adddate>='2005-6-16' and adddate <='2005-6-17'时,返回的结果只有2005-6-16的记录,却没有2005-6-17的记录; 回答:因为你的日期字段中可能有时间存在 你应该这样查询select * from news where adddate>='2005-6-16 00:00:00' and adddate <='2005-6-17 23:59:59' 或者select * from news where convert(varchar(10),adddate,120)>='2005-6-16' and convert(varchar(10),adddate,120)<='2005-6-17' 或者 select * from news where convert(varchar(10),adddate,120) between '2005-6-16' and '2005-6-17'2.sql查询语句为:select * from news where adddate>='2005-6-16'时,返回的结果正常,有2005-6-16的记录也有2005-6-17和以后的记录; 回答:adddate>='2005-6-16'当然包含2005-6-17 那天的任何时间的记录了 3.sql查询语句为:select * from news where adddate>='2005-6-16' and adddate <='2005-6-16'时,返回的结果没有记录! 回答:你上面的语句相当于 select * from news where adddate>='2005-6-16 00:00:00' and adddate <='2005-6-16 00:00:00' 你把他改为 select * from news where adddate>='2005-6-16 00:00:00' and adddate <='2005-6-16 23;59:59' 应该就有记录了
返回的结果只有2005-6-16的记录,却没有2005-6-17的记录;你这个记录的产生是这样的,你的2006-06-17的那条记录,是不是含有时:分:秒数值呀,
而且一定是大于‘2006-06-07 00:00:00’以后的,是不是??实际上系统默认你上面的查询语句,是这样的
select * from news where adddate>='2005-6-16 00:00:00' and adddate <='2005-6-17 00:00:00'如果你有记录是在2005-06-07 00:00:00 以后的,假设你有2001-06-07 01:00:00 这样的记录,
当然也就查询不出来啦
我想以下那两条语句,是同样的道理。
select * from news where convert(char(10),adddate,120)>='2005-06-16' and
convert(char(10),adddate,120) <='2005-06-17'
2.sql查询语句为:select * from news where adddate>='2005-6-16'时,返回的结果正常,有2005-6-16的记录也有2005-6-17和以后的记录;
3.sql查询语句为:select * from news where adddate>='2005-6-16' and adddate <='2005-6-16'时,返回的结果没有记录!關於這些問題都可以用類型轉換函數convert()解決
select * from news where convert(char(10),adddate,120)>='2005-06-16' and
convert(char(10),adddate,120) <='2005-06-17'
回答:因为你的日期字段中可能有时间存在
你应该这样查询select * from news where adddate>='2005-6-16 00:00:00' and adddate <='2005-6-17 23:59:59'
或者select * from news where convert(varchar(10),adddate,120)>='2005-6-16' and convert(varchar(10),adddate,120)<='2005-6-17'
或者
select * from news where convert(varchar(10),adddate,120) between '2005-6-16' and '2005-6-17'2.sql查询语句为:select * from news where adddate>='2005-6-16'时,返回的结果正常,有2005-6-16的记录也有2005-6-17和以后的记录;
回答:adddate>='2005-6-16'当然包含2005-6-17 那天的任何时间的记录了
3.sql查询语句为:select * from news where adddate>='2005-6-16' and adddate <='2005-6-16'时,返回的结果没有记录!
回答:你上面的语句相当于
select * from news where adddate>='2005-6-16 00:00:00' and adddate <='2005-6-16 00:00:00'
你把他改为
select * from news where adddate>='2005-6-16 00:00:00' and adddate <='2005-6-16 23;59:59'
应该就有记录了