在SQL Server中,也許這樣的事情
Declare @YourTable Table (RecipiantID int,Month varchar(25))
Insert Into @YourTable values
(1234,'01/2015-04/2015')
;with cteM(M) As (Select N From (Values(1),(2),(3),(4),(5),(6),(7),(8),(9),(10),(11),(12)) N(N))
,cteY(Y) As (Select 1899+Row_Number() over (Order By (Select NULL)) From cteM N1, cteM N2)
,cteD(D) As (Select DateFromParts(Y,M,1) From cteM Cross Join cteY)
Select A.RecipiantID
,Month = Month(B.D)
,ActualMonth = Format(D,'MMM-yy')
From @YourTable A
Join cteD B on D between cast(substring(Month,4,4)+'-'+left(Month,2)+'-01' as date) and cast(right(Month,4)+'-'+substring(Month,9,2)+'-01' as date)
返回
RecipiantID Month ActualMonth
1234 1 Jan-15
1234 2 Feb-15
1234 3 Mar-15
1234 4 Apr-15
您好,歡迎來SO。我們需要一些關於你的餐桌的細節來獲得真正的幫助。這裏是一個開始的好地方。 http://spaghettidba.com/2015/04/24/how-to-post-at-sql-question-on-a-public-forum/ –
如果你的數據是真的,就像你說的,狗屎,那麼它似乎很可能任何棘手的字符串操作(這對於數字表和'CROSS APPLY'的一些明智的應用來說絕對是可能的,但確實很棘手)很容易與不匹配這種崇高模式並且以驚人方式破裂的行相沖突。在適當的編程語言(或SSIS,如果你必須的話)中進行某種預處理通常遠優於T-SQL中的字符串處理。 –