2

我有多行具有不同的列值共享相同的 id 。例如

col-A   col-B       col-C        col-D                  Col-E
1       12      2012-12-01   1900-12-01 2:00:00      1900-12-01 3:30:00
2       12      2012-12-02   1900-12-01 3:00:00      1900-12-01 4:O0:00

我想最好使用分隔符( * )进入单行

12  2012-12-01 2:00 - 3:30   *    2012-12-02 3:00 - 4:00

为避免混淆 - 编辑分隔符使用 * 而不是换行符。

4

2 回答 2

3

消除[col-B]第二行是一种痛苦。以下格式日期时间是您似乎想要的方式:

select [col-B],
       (convert(varchar(19), [col-C] + [col-d], 121) + ' - ' +
        right(convert(varchar(19), [col-E], 121), 8)
       ) col
from t
于 2013-06-04T02:10:32.960 回答
1

正如其他人所指出的,这种格式实际上不是数据库问题,而是您的应用程序需要处理的问题。也就是说,感谢 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];
于 2013-06-04T03:00:32.693 回答