正如其他人所指出的,这种格式实际上不是数据库问题,而是您的应用程序需要处理的问题。也就是说,感谢 Gordon Linoff 的转换:
declare @TimeRanges as Table
( [col-A] Int Identity, [col-B] Int, [col-C] Date, [col-D] DateTime, [col-E] DateTime );
insert into @TimeRanges ( [col-B], [col-C], [col-D], [col-E] ) values
( 12, '20121201', '19001201 02:00:00', '19001201 03:30:00' ),
( 12, '20121202', '19001201 03:00:00', '19001201 04:00:00' ),
( 13, '20121219', '19001201 09:00:00', '19001201 17:00:00' );
select * from @TimeRanges;
select [col-A],
case when RN = 1 then Cast( [col-B] as VarChar(10) ) else '' end as [col-B], Range
from (
select [col-B], Row_Number() over ( partition by [col-B] order by [col-A] ) as RN,
( Convert( VarChar(19), [col-C] + [col-D], 121 ) + ' - ' +
Right( Convert( VarChar(19), [col-E], 121 ), 8 ) ) as Range
from @TimeRanges ) as ArbitraryPlaceholder
order by [col-A];