id mydate
1 2010-12-12 15:04:15
2 2010-12-22 15:05:22
3 2010-11-18 08:24:07
4 2010-11-18 17:30:58
5 2010-11-29 08:06:41
6 2010-11-29 07:38:10
7 2010-12-14 17:41:41
8 2010-12-13 16:49:18
9 2011-01-06 11:02:34其中 mydate类型为 varchar想取出日期在 2010年12月1号到2010年12月31号的记录,谢谢大家!
1 2010-12-12 15:04:15
2 2010-12-22 15:05:22
3 2010-11-18 08:24:07
4 2010-11-18 17:30:58
5 2010-11-29 08:06:41
6 2010-11-29 07:38:10
7 2010-12-14 17:41:41
8 2010-12-13 16:49:18
9 2011-01-06 11:02:34其中 mydate类型为 varchar想取出日期在 2010年12月1号到2010年12月31号的记录,谢谢大家!
select * from tb where mydate>='2010-12-01' and mydate<'2010-12-32'
select * from tb where mydate between '2010-12-1' and '2010-12-31'
select * from tb where datepart(year,madate)=2010 and datepart(MM,madate)=12
if object_id('[TB]') is not null drop table [TB]
create table [TB]([id] int,[mydate] VARCHAR(20))
insert [TB]
select 1,'2010-12-12 15:04:15' union all
select 2,'2010-12-22 15:05:22' union all
select 3,'2010-11-18 08:24:07' union all
select 4,'2010-11-18 17:30:58' union all
select 5,'2010-11-29 08:06:41' union all
select 6,'2010-11-29 07:38:10' union all
select 7,'2010-12-14 17:41:41' union all
select 8,'2010-12-13 16:49:18' union all
select 9,'2011-01-06 11:02:34'select * from [TB] WHERE mydate >'2010-12-01' AND mydate <'2010-12-31'/*
id mydate
1 2010-12-12 15:04:15
2 2010-12-22 15:05:22
7 2010-12-14 17:41:41
8 2010-12-13 16:49:18*/直接比对即可。转换也行
select * from tb where mydate between '2010-12-1' and '2010-12-31'
--or
select * from tb where datediff(day,mydate,'2010-12-1')<=0 and datediff(day,mydate,'2010-12-31')>=0