数据库是以前别人设计的 有一个字段dt是表示时间,但是设计时的数据类型是字符型现在我要把dt的时间小于当前时间的数据提取出来,该怎么写?比如;dt里为2010-8-11 今天是2010-12-3 那么就得把这条信息提取出来 SQL语句该怎么写?
string qstr="select * from table where dt<='"+datetime.now.data()+"'"; ?急,帮帮忙!!!c#.net

解决方案 »

  1.   

    select   convert(datetime,dt,120)   as   dtTime   from   table where dtTime <='"+datetime.now.data()+"'"; 你想要的是SQL语句还是LINQ语句?
      

  2.   


    string qstr="select * from table where dt<=getdate()";
      

  3.   

    select convert(datetime,dt,120) as dtTime from table where convert(datetime,dt,120) <getdate() 
    select convert(datetime,dt,120) as dtTime from table where datediff(d,convert(datetime,dt,120),getdate)>0
      

  4.   

    #1. 字符串对比,要求格式一致
    #2. dt字段转换成datetime类型,再对比
    SELECT * FROM table_name WHERE CAST(dt AS DATETIME) < GETDATE()
      

  5.   

    SELECT * from TB Where Convert(DateTime,dt,120) <GetDate() 
      

  6.   

    select convert(varchar(10),dt,120) as dtTime from table where convert(varchar(10),dt,120) <=convert(varchar(10),getdate(),120)
      

  7.   

    select * from table where convert(datetime,dt) < getdate()
      

  8.   


    string qstr="select * from table where cast(dt as datetime) < getdate() "
      

  9.   

    你可以在前段程序里提取出时间然后做比较,也可以用sqlserver的getdate() (sqlserver 所在服务器的时间)直接在sqlserver里做比较,当然前提你的sqlserver所在服务器时间是正确的,这个取决于你的程序架构,比如你想比对的时间是和客户机(本机)比还是服务器时间
    select * from tb where convert (datetime,col)<getdate()