日期:2014-05-18  浏览次数:20478 次

求一SQL语句或一种解决方法
table1
---------------------------------
序号           名称             日期                
1                   A             2007-1-22
2                   B             2007-1-25        
3                   C                 待办
4                   D                 未填
......
---------------------------------
如上表,想按日期进行筛选,把一段时间内的记录检索出来,由于日期字段为字符型,所以检索起来比较麻烦。能否一条SQL语句可以解决,或其他解决方法也行,尽量实现起来简单为好。

------解决方案--------------------
select * from table1
where isnumeric(left(日期,1)) = 1
and convert(datetime,日期,120) between 开始日期 and 结束日期
------解决方案--------------------
select * from table1
where isdate(日期) = 1
and convert(datetime,日期,120) between 开始日期 and 结束日期

*****************************************************************************
欢迎使用CSDN论坛专用阅读器 : CSDN Reader(附全部源代码)

最新版本:20070130

http://www.cnblogs.com/feiyun0112/archive/2006/09/20/509783.html
------解决方案--------------------
--try

create table T(序号 int, 名称 varchar(10), 日期 varchar(20))
insert T select 1, 'A ', '2007-1-22 '
union all select 2, 'B ', '2007-1-25 '
union all select 3, 'C ', '待办 '
union all select 4, 'D ', '未填 '

select * from T
where (case when isdate(日期)=1 then cast(日期 as datetime) else NULL end)
between '2007-01-21 ' and '2007-01-23 '

--result
序号 名称 日期
----------- ---------- --------------------
1 A 2007-1-22

(1 row(s) affected)