写一条sql语句将12:00:00之前分为一列AM,12:00:00之后为pm
time
2010-06-06 8:00:00
2010-06-06 12:00:00
2010-06-07 8:00:00
2010-06-07 12:00:00
2010-06-08 7:00:00
2010-06-08 13:00:00查询以后为
      am                              pm
2010-06-06 8:00:00           2010-06-06 12:00:00
2010-06-07 8:00:00           2010-06-07 12:00:00 
2010-06-08 7:00:00           2010-06-08 13:00:00 

解决方案 »

  1.   

    select time am,convert(varchar(11),time,120)+convert(varchar(10),time,108) pm from [Table]
    where datediff(s,time,convert(varchar(11),time,120)+'12:00:00')>0
      

  2.   

    select 
    max(case when datepart(hh,time)<12 then time else null end) am,
    max(case when datepart(hh,time)>=12 then time else null end) pm
    from tb group by convert(varchar(10),time,23)
      

  3.   

    select a.time am ,b.time pm from(select time from table where datepart(hh,time)<12)a
    full join (select time from table where datepart(hh,time)>=12) on convert(varchar(10),a.time,120)=convert(varchar(10),b.time,120)
      

  4.   

    ---测试数据---
    if object_id('[tb]') is not null drop table [tb]
    go
    create table [tb]([time] datetime)
    insert [tb]
    select '2010-06-06 8:00:00' union all
    select '2010-06-06 12:00:00' union all
    select '2010-06-07 8:00:00' union all
    select '2010-06-07 12:00:00' union all
    select '2010-06-08 7:00:00' union all
    select '2010-06-08 13:00:00'
     
    ---查询---
    select 
    am=max(case when convert(varchar(8),[time],108)<'12:00:00' then [time] end),
    pm=max(case when convert(varchar(8),[time],108)>='12:00:00' then [time] end)
    from tb
    group by convert(varchar(10),[time],120)---结果---
    am                      pm
    ----------------------- -----------------------
    2010-06-06 08:00:00.000 2010-06-06 12:00:00.000
    2010-06-07 08:00:00.000 2010-06-07 12:00:00.000
    2010-06-08 07:00:00.000 2010-06-08 13:00:00.000
    警告: 聚合或其他 SET 操作消除了空值。(3 行受影响)
      

  5.   

    select time am, (select convert(varchar(11),time,120)+convert(varchar(10),time,108) pm FROM @a WHERE DATEDIFF(DAY,TIME,a.time)=0 AND DATEDIFF(s,a.TIME,time)>0) from [Table] a
    where datediff(s,time,convert(varchar(11),time,120)+'12:00:00')>0
      

  6.   


    create table tb (date_time datetime)
    insert into tb
    select '2010-06-06 8:00:00' union all
    select '2010-06-06 12:00:00' union all
    select '2010-06-07 8:00:00' union all
    select '2010-06-07 12:00:00' union all
    select '2010-06-08 7:00:00' union all
    select '2010-06-08 13:00:00';with t
    as
    (
    select (case  when CONVERT(varchar(12) , date_time, 108 )<'12:00:00' then date_time else null end) AM,
       (case  when CONVERT(varchar(12) , date_time, 108 )>='12:00:00' then date_time else null end) PM
    from tb
    )
    select AM,PM
    from (select am 
          from t 
          where AM is not null) a full join (select pm 
                                             from t 
                                             where PM is not null)b 
         on CONVERT(varchar(10),AM,120)=CONVERT(varchar(10),pM,120)
    AM PM
    2010-06-06 08:00:00.000 2010-06-06 12:00:00.000
    2010-06-07 08:00:00.000 2010-06-07 12:00:00.000
    2010-06-08 07:00:00.000 2010-06-08 13:00:00.000
      

  7.   


    create table #tb(dt datetime)
    insert into #tb
    select '2010-06-06 8:00:00' union all
    select '2010-06-06 12:00:00' union all
    select '2010-06-07 8:00:00' union all
    select '2010-06-07 12:00:00' union all
    select '2010-06-08 7:00:00' union all
    select '2010-06-08 13:00:00' select am,pm
    from (select convert(varchar(10),dt,121)+'  '+case when convert(varchar(10),dt,108)<'12:00:00' then convert(varchar(10),dt,108) end am
    from #tb
    where convert(varchar(10),dt,121)+'  '+case when convert(varchar(10),dt,108)<'12:00:00' then convert(varchar(10),dt,108) end is not null) a,
    (select convert(varchar(10),dt,121)+'  '+case when convert(varchar(10),dt,108)>='12:00:00' then convert(varchar(10),dt,108) end pm
    from #tb
    where convert(varchar(10),dt,121)+'  '+case when convert(varchar(10),dt,108)>='12:00:00' then convert(varchar(10),dt,108) end is not null) b 
    where convert(char(10),a.am,23)=convert(char(10),b.pm,23)
    am                      pm
    2010-06-06  08:00:00 2010-06-06  12:00:00
    2010-06-07  08:00:00 2010-06-07  12:00:00
    2010-06-08  07:00:00 2010-06-08  13:00:00