我在sql server2000下面有个数据库表birth,两列
name char(12),
birthday smalldatetime怎么用insert语句插入一条纪录,比如“张三,1980-4-5”
insert into birth
values (‘张三’,1980-4-5)
这么插入后,发现日期完全不对了,要怎么写呢?另外,表中已经有了纪录,怎么得到今天过生日者的年龄?

解决方案 »

  1.   

    INSERT birth values (‘张三’,'1980-4-5') 
    select * from birth where datediff(day,birth,getdate())=0
      

  2.   

    INSERT birth values (‘张三’,‘1980-4-5’) 
    select * from birth where datediff(day,birth,getdate())=0
    把引号换成英文状态的
      

  3.   

    insert into birth values('张三','1980-4-5')select name , 今天过生日者的年龄 = datediff(year,smalldatetime,getdate()) from birth where datepart(month,samlldatetime,getdate()) = 0 and datepart(day,smalldatetime,getdate()) = 0
      

  4.   

    --插入
    insert birth select ('张三','1980-4-5')
    --查找今天过生日的用户的年龄
    select *,datediff(yy,birthday,getdate()) from tb where datediff(dd,birthday,getdate())=0
      

  5.   

    上面弄错了,不好意思--插入
    insert birth select ('张三','1980-4-5')
    --查找今天过生日的用户的年龄
    select *,datediff(yy,birthday,getdate()) from tb 
      where sutff(convert(varchar(10),birthday,120),1,4,'')=
             sutff(convert(varchar(10),getdate(),120),1,4,'')
    --用datepart或month,day等函数处理也可以
      

  6.   

    create table birth(name char(12),birthday smalldatetime)--插入方法一
    insert into birth values('张三','1980-4-5')
    --插入方法2
    insert into birth select '李四','1983-5-6'
    --插入方法3
    insert into birth(name,birthday) values('张三','1980-10-31')--获取今天过生日的人的年龄 getdate()获得的日期为2007-10-31
    select *,year(getdate())-year(birthday) as 年龄 from birth
    where month(birthday)=month(getdate())
    and day(birthday)=day(getdate())
    /*
    张三         1980-10-31 00:00:00 27*/
      

  7.   

    在日期上面加上单引号然后插入结果就是正确的用datediff函数可以得到今天过生日的人,但是我同时要把他的岁数算出来,怎么算?
      

  8.   

    renzhe02 的解答很精彩,谢谢我还想问一下,如果用一条insert语句插入两条以上纪录怎么写
    比如:我写
    insert into birth 
      select 
        '李四','1983-5-6'
        '张三','1980-4-5'
    似乎不行!
      

  9.   

    insert   into   birth   
        select     '李四 ', '1983-5-6 &apos
    union 
         select    '张三 ', '1980-4-5 &aposFROM (select top 1 * from syscolumns) a
      

  10.   

    日期两边加单引号。。
    年龄:本日减去数据库中的日期
    select * from  birth where datediff(day,birth,getdate())=0
      

  11.   


    create table birth(
        name char(12),
        birthday smalldatetime
    )
    go
    insert into birth values('张三','1981-4-5')
    insert into birth values('李四','1981-11-1')select *,datediff(yy,birthday,getdate()) as age from birth
    where datepart(mm,birthday)-datepart(mm,getdate())=0 and datepart(dd,birthday)-datepart(dd,getdate())=0drop table birth/****
    name         birthday                                               age         
    ------------ ------------------------------------------------------ ----------- 
    李四           1981-11-01 00:00:00                                    26
    ****/
      

  12.   

    借用楼上数据:create table birth(
        name char(12),
        birthday smalldatetime
    )
    go
    insert into birth values('张三','1981-4-5')
    insert into birth values('李四','1981-11-1')go
    select 
    *
    from 
    birth
    where
    datediff(d,dateadd(yy,datediff(yy,birthday,getdate()),birthday),getdate())=0name         birthday                                               
    ------------ ------------------------------------------------------ 
    李四           1981-11-01 00:00:00(所影响的行数为 1 行)
      

  13.   


    --查当月
    select 
    *
    from 
    birth
    where
    datediff(m,dateadd(yy,datediff(yy,birthday,getdate()),birthday),getdate())=0
      

  14.   

    Declare @tb table(name1 nvarchar(20),birthday smalldatetime)
    insert into @tb 
    select '张三','1981-4-5' union
    select '李四','1981-11-1'select name1,birthday,datediff(yy,birthday,getdate())as age from @tb
    where datepart(mm,birthday)-datepart(mm,getdate()) = 0 
    and datepart(dd,birthday)- datepart(dd,getdate()) = 0
      

  15.   

    create table birth
    (name char(8),
     birthday smalldatetime 
    );
    insert into birth values('张三','1980-4-5')
       这样插入就可以了啊 
      

  16.   

    T-SQL: 17 个与日期时间相关的自定义函数(UDF),周日作为周的最后一天,均不受 @@DateFirst、语言版本影响! 
    http://www.cnblogs.com/Microshaoft/archive/2005/04/26/145334.html
    create function udf_GetAge(@StartDate datetime,@EndDate datetime) 
    returns integer 
    -- 返回精确年龄 select dbo.udf_GetAge('1949-10-01',getdate()) 
    begin 
    return datediff(year,@StartDate,@EndDate) 
           - case when datediff(day,dateadd(year,datediff(year,@StartDate,@EndDate),@StartDate),@EndDate) >= 0 
                       then 0 
                  else 
                       1 
             end 
    end
      

  17.   

    insert birth select ('张三','1980-4-5')
      

  18.   

    今天过生日的
    month(birth)=month(getdate()) and day(birth)=day(getdate())