数据库里记录的时间格式为
05  5 2011  9:45PM
05 10 2011  8:00AM字段类型为:varchar型。现在改字段类型,怕出问题。在查询的时候应该怎么查呢?比如:
select * from table where times>='05  5 2011  9:45PM' and times<='05 10 2011  8:00AM'.这样查询,数据是肯定查不出来的,应该怎么样呢?望各路高手帮忙解决。

解决方案 »

  1.   

    convert(datetime,'05 5 2011 9:45PM',109)
      

  2.   

    where CAST(times as datetime) between '2011-05-05 09:45:00.000' and '2011-05-10 08:00:00.000'
      

  3.   

    cast(时间字段 as datetime)
      

  4.   

    使用格式转换函数
    1.convert
    2.cast
      

  5.   

    select * from table where 
    cast(replace(rtrim(left(times,10)),' ','/')+' '+ltrim(right(times,7)) as datetime)>='2011-05-05 21:45:00' 
    and 
    cast(replace(rtrim(left(times,10)),' ','/')+' '+ltrim(right(times,7)) as datetime)><'2011-05-10 08:00:00'
      

  6.   

    declare @s varchar(20)
    set @s='05 5 2011 9:45PM'select convert(varchar(16),CAST(@s as datetime),120)
    /*----------------
    2011-05-05 21:45(1 行受影响)*/ 
      

  7.   

    declare @s varchar(20)
    set @s='05 5 2011 9:45PM'select convert(varchar(16),CAST(@s as datetime),120)
    /*----------------
    2011-05-05 21:45(1 行受影响)*/