正如其他人所指出的,這種格式的真的不是數據庫的問題,但有些事您的應用程序來處理。這就是說,並感謝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];
col-C D和E的數據類型是什麼?日期,日期時間或Varchar? – sgeddes
如果此查詢由諸如.net,coldfusion,java等應用程序代碼調用,運行一個非常簡單的查詢來獲取數據並使用應用程序代碼對其進行格式化可能會更容易。 –
SQL用於處理數據,而不是顯示它。在客戶端代碼中做這種事。 –