例如:
2010-08-30与1988-4-6相差多少天?多少月?
怎么写代码啊?

解决方案 »

  1.   

    datediff(day,startdate,enddate)
    datediff(month,startdate,enddate)
      

  2.   

    if object_id('[tb]') is not null drop table [tb]
    go
    create table [tb]([col1] datetime,[col2] datetime)
    insert [tb]
    select '2010-08-30','1988-4-6'
    goselect
      datediff(dd,col2,col1) as 相差天数,
      datediff(mm,col2,col1) as 相差月数
    from
      tb /**
    相差天数        相差月数
    ----------- -----------
    8181        268(1 行受影响)
    **/
      

  3.   


    select DATEDIFF(month ,'1988-4-6','2010-08-30'),
        DATEDIFF(day  ,'1988-4-6','2010-08-30')
      

  4.   

    select datediff(day,'1988-4-6','2010-08-30')--8181 daysselect datediff(month,'1988-4-6','2010-08-30')--268 months