我在用insert语句 向数据库插入一条数据,日期写“2011-1-11”时,数据库中却显示为“1905-6-22 0:00:00”我 把日期改为“2011-1-11 18:23:23”时,却提示 18附近有语法错误。请问高手 这是怎么搞的

解决方案 »

  1.   

    --> 测试数据: #tb
    if object_id('tempdb.dbo.#tb') is not null drop table #tb
    go 
    create table #tb (id int,name datetime)
    insert into #tb
    select 1,'2011-1-11 18:23:23'  select * from #tb
    id          name
    ----------- -----------------------
    1           2011-01-11 18:23:23.000(1 行受影响)
      

  2.   


    --不知道你语句是怎么写的!
    create table b(dat datetime)
    insert into b
    select '2010-1-12'
    goinsert into b select '2010-1-1 18:30'
    select * from bdrop table bdat
    -----------------------
    2010-01-12 00:00:00.000
    2010-01-01 18:30:00.000
      

  3.   

    我只是在表中建立了一个datetime型的字段,在页面中点击确认按钮时,想让系统的时间输入该表的datetime字段。我在页面的代码中用的是DateFormat df = null;
                               df = DateFormat.getDateTimeInstance();
    然后再用insert into DepotMission (orderID,stateID,depotMissionTime,billID) values ("+orderID+",1,"+df.format(new Date()).toString()+",1) 其他没问题,df.format(new Date()).toString() 得到的结果是“2011-1-11 18:33:58”
      

  4.   

    直接用个string来保存当前时间,就可以了
      

  5.   

    不是吧?
    create table tb(dt datetime)
    insert into tb values('2011-1-11')
    insert into tb values('2011-1-11 18:23:23')
    goselect * from tbdrop table tb/*
    dt                                                     
    ------------------------------------------------------ 
    2011-01-11 00:00:00.000
    2011-01-11 18:23:23.000(所影响的行数为 2 行)
    */
      

  6.   

    DateFormat d1 = DateFormat.getDateInstance(); 
    String str1 = d1.format(now);
      

  7.   

    insert into DepotMission (orderID,stateID,depotMissionTime,billID) values ("+orderID+",1,"+str1+",1)
      

  8.   


    类似。 我找到问题了。日期型必须要加单引号,我在页面中把语句改为
    insert into DepotMission (orderID,stateID,depotMissionTime,billID) values ("+orderID+",1,"+“‘”+df.format(new Date()).toString()+"’,1)就可以了呵呵,基本功太烂了