数据库的outdate是char 如 2007年10月25日 2009年07月04日 都是这样的
我要在数据库里实现 between  2007-10-25 and 2009-05-01
这样导出hwouttb3insert into hwouttb3
select * from hwouttb where outdate between '2007年10月25日' and '2007年11月25日'and gysh like '%生產%' or gysh like '%工程%' or gysh like '%倉庫%'
union all
select * from hwouttb2 where outdate between '2007年10月25日' and '2007年11月25日'and gysh like '%生產%' or gysh like '%工程%' or gysh like '%倉庫%'刚才有个兄弟写了个
没有在between and范围内insert into hwouttb3 
select * from hwouttb where 
CAST(REPLACE(REPLACE(REPLACE(outdate,'年','-' ),'月','-'),'日','') AS DATETIME)
between 
CAST(REPLACE(REPLACE(REPLACE('2007年10月25日','年','-' ),'月','-'),'日','') AS DATETIME)
and 
CAST(REPLACE(REPLACE(REPLACE('2007年11月25日','年','-' ),'月','-'),'日','') AS DATETIME)
and gysh like '%生產%' or gysh like '%工程%' or gysh like '%倉庫%' 
union all 
select * from hwouttb2 where 
CAST(REPLACE(REPLACE(REPLACE(outdate,'年','-' ),'月','-'),'日','') AS DATETIME) 
between 
CAST(REPLACE(REPLACE(REPLACE('2007年10月25日','年','-' ),'月','-'),'日','') AS DATETIME)
and 
CAST(REPLACE(REPLACE(REPLACE('2007年11月25日','年','-' ),'月','-'),'日','') AS DATETIME)
and gysh like '%生產%' or gysh like '%工程%' or gysh like '%倉庫%'

解决方案 »

  1.   

    你存储的时候存储成中文那样的格式,那个应该没错呀,看样子你是想要最后那个日期的所有数据是吧,
    直接把天数加一天,或用DATEFIFF
      

  2.   

    我想不明白between and条件没配上
     输入来的数据有2008年的数据和2009的数据还有些2007不是我条件的数据!
      
      

  3.   

    可以贴出来你的有问题的搜索结果,那个sql应该是没问题的
      

  4.   

    刚学不久,,,向LZ  学习,,做了一下练习。。
    use kiaqew
    /*create table rq
    (sid char(10),
    srq  varchar(15)
    )
    go
    insert into rq values('1','2006年10月25日');
    insert into rq values('2','2005年10月25日');
    insert into rq values('3','2007年11月25日');
    insert into rq values('4','2007年1月25日');
    insert into rq values('5','2007年9月25日');
    insert into rq values('6','2007年2月25日');
    insert into rq values('7','2007年3月25日');
    insert into rq values('8','2007年5月25日');
    insert into rq values('10','2007年6月25日');
    */
    select CAST(REPLACE(REPLACE(REPLACE(srq,'年','-' ),'月','-'),'日','') AS DATETIME) as rqtime,* into #kkkk from rq 
    select * from #kkkk where rqtime between  '2007-1-1' and '2007-12-1'可以成功。。学习了。。replace ..哈
      

  5.   

    改了下终于可以了就少了个括号select * from hwouttb where 
    CAST(REPLACE(REPLACE(REPLACE(outdate,'年','-' ),'月','-'),'日','') AS DATETIME)
    between 
    CAST(REPLACE(REPLACE(REPLACE('2007年10月25日','年','-' ),'月','-'),'日','') AS DATETIME)
    and 
    CAST(REPLACE(REPLACE(REPLACE('2007年10月25日','年','-' ),'月','-'),'日','') AS DATETIME)
    and (gysh like '%生產%' or gysh like '%工程%' or gysh like '%倉庫%') 
    union all 
    select * from hwouttb2 where 
    CAST(REPLACE(REPLACE(REPLACE(outdate,'年','-' ),'月','-'),'日','') AS DATETIME) 
    between 
    CAST(REPLACE(REPLACE(REPLACE('2007年10月25日','年','-' ),'月','-'),'日','') AS DATETIME)
    and 
    CAST(REPLACE(REPLACE(REPLACE('2007年10月25日','年','-' ),'月','-'),'日','') AS DATETIME)
    and (gysh like '%生產%' or gysh like '%工程%' or gysh like '%倉庫%')