数据库里面字段是 varchar类型 但格式是日期格式 2009-04-02 怎么和系统时间判断 sql的语法怎么写

解决方案 »

  1.   

    SQL强制转换格式吧..用那个函数忘记了~看看帮助~
      

  2.   

    convert(varchar(10),[datetiem],120)
    可以转换成2009-04-02这种格式
      

  3.   

    cast('2009-04-02' as datetime)
      

  4.   

    通过Convert.ToDateTime()来转化呀。。
      

  5.   


    --非要转换的话,加入2009-04-02 这个字段是dtColumn
    select convert(datetime,dtColumn) from tableA
      

  6.   

    convert(转化成的类型,字段,格式) 格式如下
    不带世纪数位 (yy) (1)  带世纪数位 (yyyy)  标准  输入/输出 (3)  
    -
     0 或 100 (1, 2) 
     默认设置
     mon dd yyyy hh:miAM(或 PM)
     
    1
     101
     美国
     mm/dd/yyyy
     
    2
     102
     ANSI
     yy.mm.dd
     
    3
     103
     英国/法国
     dd/mm/yy
     
    4
     104
     德国
     dd.mm.yy
     
    5
     105
     意大利
     dd-mm-yy
     
    6
     106 (1)
     -
     dd mon yy
     
    7
     107 (1)
     -
     mon dd, yy
     
    8
     108
     -
     hh:mm:ss
     
    -
     9 或 109 (1, 2) 
     默认设置 + 毫秒
     mon dd yyyy hh:mi:ss:mmmAM(或 PM)
     
    10
     110
     美国
     mm-dd-yy
     
    11
     111
     日本
     yy/mm/dd
     
    12
     112
     ISO
     yymmdd
     
    -
     13 或 113 (1, 2) 
     欧洲默认设置 + 毫秒
     dd mon yyyy hh:mm:ss:mmm(24h)
     
    14
     114
     -
     hh:mi:ss:mmm(24h)
     
    -
     20 或 120 (2) 
     ODBC 规范
     yyyy-mm-dd hh:mi:ss(24h)
     
    -
     21 或 121 (2) 
     ODBC 规范(带毫秒)
     yyyy-mm-dd hh:mi:ss.mmm(24h)
     
    -
     126 (4)
     ISO8601
     yyyy-mm-ddThh:mm:ss.mmm(无空格)
     
     127(6)
     带时区 Z 的 ISO8601。
     yyyy-mm-ddThh:mm:ss.mmmZ (无空格)
     
    -
     130 (1, 2)
     回历 (5)
     dd mon yyyy hh:mi:ss:mmmAM
     
    -
     131 (2)
     回历 (5)
     dd/mm/yy hh:mi:ss:mmmAM
     
      

  7.   


    DECLARE @dt1 VARCHAR(10)
    DECLARE @dt2 DATETIME
    SET @dt1 = '2009-04-02'
    SET @dt2 = GETDATE()select @dt1 = replace(@dt1,'-','')
    select @dt2 = convert(char(8), @dt1, 112)if(@dt1 > @dt2 ) 
     print '1'
    else print '2'
      

  8.   

    你只要把系统时间转换成 2009-04-02 这种格式就可以判断了,数据中的字段不需要转换,varchar型直接可以判断的
      

  9.   


    刚写错了
    你这个值已经是日期格式了,不用转换!
    非要转换的话,试试下面的SQL--非要转换的话,假如2009-04-02 这个字段是dtColumn
    select convert(datetime,dtColumn) from tableA
      

  10.   

    假设字段名是:addDate varchar(10),表名是MyTable,SQL语句如下:
    select * from MyTable where convert(varchar(10),datetiem,120)<getDate()
    查询添加小于今天的记录(其实就是所有的记录)。话说回来,用varchar来存储日期时间,这种数据库设计本身就有问题,每次查询都需要查询转换,效率很低。
      

  11.   

    --假如2009-04-02 这个字段是dtColumn (varchar),给它加10天,
    select dateadd(day,10,convert(datetime,dtColumn)) from tableA
      

  12.   


    不知道你的判断一下是什么意思?
    比如与当前系统时间再加10天比较,返回日期小于当前系统时间加10天的所有数据,
    可以参考如下SQL代码:--假如2009-04-02 这个字段是dtColumn (varchar),查询小于系统时间加10天的所有数据
    select * from tableA where convert(datetime,dtColumn)<dateadd(day,10,getdate())
      

  13.   

    where convert(varchar(10),你的那个varchar字段,120)<getDate()
      

  14.   

    可以简单写成这样:
    declare @dt varchar(10)
    set @dt='2009-03-08'if @dt<getdate()+10
    print 'do sth.'
    else 
    print 'do sth. else'
    系统会自动做一些隐式转换。
      

  15.   

    declare @Date varchar(30)
    set @Date='2009-04-02'
    if (Convert(varchar(10),@Date,120)<=getdate())
    begin
       print '小于等于'
    end
    else
    begin
        print '大于'
    end
      

  16.   


    select * from table where convert(varchar(10),[datetiem],120) =convert(varchar(10),getdate(),120)
      

  17.   

    declare @Date varchar(30)
    set @Date='2009-04-02'
    if (Convert(varchar(10),@Date,120)<=dateadd(day,10,getdate()))
    begin
       print '小于等于'
    end
    else
    begin
        print '大于'
    end
      

  18.   


    select * from table where convert(varchar(10),[datetiem],120) =convert(varchar(10),dateadd(day,10,getdate(),120)
      

  19.   

    select * from MyTable where convert(varchar(10),datetiem,120)<getDate()
      

  20.   

    convert和cast好相都要以。
    可以自己去看下幫助
      

  21.   

    DateTime.Parse (this.日期.Text).ToString("yyyyMMdd");
    这样转换
      

  22.   


    使用 CONVERT:
    CONVERT (data_type[(length)], expression [, style])select CONVERT(varchar, getdate(), 120 ) 
    2004-09-12 11:06:08 
    select replace(replace(replace(CONVERT(varchar, getdate(), 120 ),"-","")," ",""),":","") 
    20040912110608 
    select CONVERT(varchar(12) , getdate(), 111 ) 
    2004/09/12 
    select CONVERT(varchar(12) , getdate(), 112 ) 
    20040912 
    select CONVERT(varchar(12) , getdate(), 102 ) 
    2004.09.12 
    select CONVERT(varchar(12) , getdate(), 101 ) 
    09/12/2004 
    select CONVERT(varchar(12) , getdate(), 103 ) 
    12/09/2004 
    select CONVERT(varchar(12) , getdate(), 104 ) 
    12.09.2004 
    select CONVERT(varchar(12) , getdate(), 105 ) 
    12-09-2004 
    select CONVERT(varchar(12) , getdate(), 106 ) 
    12 09 2004 
    select CONVERT(varchar(12) , getdate(), 107 ) 
    09 12, 2004 
    select CONVERT(varchar(12) , getdate(), 108 ) 
    11:06:08 
    select CONVERT(varchar(12) , getdate(), 109 ) 
    09 12 2004 1 
    select CONVERT(varchar(12) , getdate(), 110 ) 
    09-12-2004 
    select CONVERT(varchar(12) , getdate(), 113 ) 
    12 09 2004 1 
    select CONVERT(varchar(12) , getdate(), 114 ) 
    11:06:08.177
      

  23.   

    cast('2009-04-02' as datetime)
      

  24.   

    时间字段还是用datetime来存储,用varchar效率很低。
      

  25.   

    select * from tb where times=cast('2009-04-03' as datetime)
      

  26.   

    convert(varchar(10),[datetiem],120) 
    可以转换成2009-04-02这种格式
      

  27.   


    select * from 表名 where datediff(dd,'2009-04-02',getdate())=0
      

  28.   

    select convert(varchar(10),getdate() ,120)  
    ----------
    2009-04-09