create table #t1 
(
bh varchar(10) not null,
xm varchar(20) not null,
rq1 int null,
rq2 int null
)insert #t1(bh,xm,rq1)
select '001','张三',39216
insert #t1(bh,xm,rq1,rq2)
select '002','李四',39216,39220----此种运行不能得到正确结果
select bh,xm,
rq1=dateadd(day,rq1,'1899.12.30'),
rq2=case when rq2 is null then '' else dateadd(day,rq2,'1899.12.30') end
from #t1---要得到如下结果:
bh       xm         rq1                rq2
001 张三 2007-05-14       
002 李四 2007-05-14 2007-05-18

解决方案 »

  1.   

    select bh,xm,
    rq1=dateadd(day,rq1,'1899.12.30'),
    rq2=case when rq2 is null then null else dateadd(day,rq2,'1899.12.30') end
    from #t1
      

  2.   

    select bh,xm,
    rq1=dateadd(day,rq1,'1899.12.30'),
    rq2=case when rq2 is null then '' else convert(varchar(10),dateadd(day,rq2,'1899.12.30'),120) end
    from #t1
      

  3.   

    select bh,xm,
    rq1=dateadd(day,rq1,'1899.12.30'),
    rq2=isnull(convert(varchar(25),dateadd(day,rq2,'1899.12.30'),121),'')
    from #t1
      

  4.   

    create table #t1 
    (
    bh varchar(10) not null,
    xm varchar(20) not null,
    rq1 int null,
    rq2 int null
    )insert #t1(bh,xm,rq1)
    select '001','张三',39216
    insert #t1(bh,xm,rq1,rq2)
    select '002','李四',39216,39220select * from #t1select bh,xm,
    rq1=convert(char(10),dateadd(day,rq1,'1899.12.30'),120),
    rq2=case when rq2 is null then '' else convert(char(10),dateadd(day,rq2,'1899.12.30'),120) end
    from #t1drop table #t1
      

  5.   

    create table #t1 
    (
    bh varchar(10) not null,
    xm varchar(20) not null,
    rq1 int null,
    rq2 int null
    )insert #t1(bh,xm,rq1)
    select '001','张三',39216
    insert #t1(bh,xm,rq1,rq2)
    select '002','李四',39216,39220select bh,xm,
    rq1=convert(char(10),dateadd(day,rq1,'1899.12.30'),120),
    rq2=case when rq2 is null then '' 
    else convert(char(10),dateadd(day,rq2,'1899.12.30'),120) end
    from #t1drop table #t1