我的下表中提到了下面提到的值。我有一个标签,显示表格中的日期。第一个日期应显示为07/06/2011,第二个日期应显示如下格式,任何人帮助我..
“标签:07/07/2011至07/13/2011”,
ID Date
== ==========
63 07/06/2011
64 07/13/2011
65 07/20/2011
66 07/27/2011
67 08/03/2011
答案 0 :(得分:1)
试试这个:
WITH qry AS
(
SELECT a.*,
ROW_NUMBER() OVER(ORDER BY Date) rn
FROM MyTable a
)
SELECT a.Id,
CASE
WHEN b.Id IS NULL THEN CONVERT(VARCHAR, a.Date, 101)
ELSE 'Label: ' +
CONVERT(VARCHAR, b.Date + 1, 101) +
' to ' +
CONVERT(VARCHAR, a.Date , 101)
END AS Date
FROM qry a LEFT JOIN qry b
ON a.rn = b.rn+1
表格设置:
CREATE TABLE MyTable (ID INT, DATE DATETIME)
INSERT INTO MyTable VALUES(63,'07/06/2011');
INSERT INTO MyTable VALUES(64,'07/13/2011');
INSERT INTO MyTable VALUES(65,'07/20/2011');
INSERT INTO MyTable VALUES(66,'07/27/2011');
INSERT INTO MyTable VALUES(67,'08/03/2011');
结果:
Id Date
63 07/06/2011
64 Label: 07/07/2011 to 07/13/2011
65 Label: 07/14/2011 to 07/20/2011
66 Label: 07/21/2011 to 07/27/2011
67 Label: 07/28/2011 to 08/03/2011
答案 1 :(得分:0)
根据您的日/月格式,您可以尝试:
SELECT
CONVERT(VARCHAR, [date] , 4)