基本步驟:
- 使用獲取的數據插入到表中的SQL Server SSIS
- 殺死的「課程 - 級學習目標」龍
- 逆透視結果
下面是一個代碼片段‘第N個’指數函數:
CREATE FUNCTION [dbo].[udf_NthIndex]
(@Input VARCHAR(8000),
@Delimiter CHAR(1),
@Ordinal INT)
RETURNS INT
AS
BEGIN
DECLARE @Pointer INT,
@Last INT,
@Count INT
SET @Pointer = 1
SET @Last = 0
SET @Count = 1
WHILE (2 > 1)
BEGIN
SET @Pointer = CHARINDEX(@Delimiter,@Input,@Pointer)
IF @Pointer = 0
BREAK
IF @Count = @Ordinal
BEGIN
SET @Last = @Pointer
BREAK
END
SET @Count = @Count + 1
SET @Pointer = @Pointer + 1
END
RETURN @Last
END
GO
;
這種方法,用Common Table Expressions解決,「第n」指數函數,UNPIVOT
WITH s1
AS (SELECT 'ABE 095' AS [Prefix/Code]
, 'Keys to Academic Success' AS Name
, '3.0' AS Credits
, 'Basic .. assessment. ' AS Description
, '
Identify learn. [EXPLORE]
Evaluate personal, goals. [ACT]
Utilize development. [EXPLORE]
' AS [Course-level Learning Objectives]
) , s2
AS (SELECT [Prefix/Code]
, Name
, Credits
, Description
, dbo.udf_NthIndex([Course-level Learning Objectives] , CHAR(13
) , 2
) + 2 AS Type1Start
, dbo.udf_NthIndex([Course-level Learning Objectives] , CHAR(13
) , 3
) - dbo.udf_NthIndex([Course-level Learning Objectives] , CHAR(13
) , 2
) + 0 AS Type1Length
, dbo.udf_NthIndex([Course-level Learning Objectives] , CHAR(13
) , 3
) + 2 AS Type2Start
, dbo.udf_NthIndex([Course-level Learning Objectives] , CHAR(13
) , 4
) - dbo.udf_NthIndex([Course-level Learning Objectives] , CHAR(13
) , 3
) + 0 AS Type2Length
, dbo.udf_NthIndex([Course-level Learning Objectives] , CHAR(13
) , 4
) + 2 AS Type3Start
, dbo.udf_NthIndex([Course-level Learning Objectives] , CHAR(13
) , 5
) - dbo.udf_NthIndex([Course-level Learning Objectives] , CHAR(13
) , 4
) + 0 AS Type3Length
FROM s1
) , s3
AS (SELECT s2.[Prefix/Code]
, s2.Name
, s2.Credits
, s2.Description
, RTRIM(LTRIM(SUBSTRING(s1.[Course-level Learning Objectives] , s2.Type1Start , Type1Length
)
)
)AS Type1_chunk
, RTRIM(LTRIM(SUBSTRING(s1.[Course-level Learning Objectives] , s2.Type2Start , Type2Length
)
)
)AS Type2_chunk
, RTRIM(LTRIM(SUBSTRING(s1.[Course-level Learning Objectives] , s2.Type3Start , Type3Length
)
)
)AS Type3_chunk
FROM s1 , s2
) , unpivot1
AS (SELECT [Prefix/Code]
, Name
, Credits
, Description
, Type_chunk
FROM(
SELECT [Prefix/Code]
, Name
, Credits
, Description
, Type1_chunk
, Type2_chunk
, Type3_chunk
FROM s3
)p UNPIVOT(Type_chunk FOR Type_descrip IN(Type1_chunk
, Type2_chunk
, Type3_chunk
)
)AS unpvt
)
SELECT [Prefix/Code]
, Name
, Credits
, Description
--, Type_chunk
, LEFT(u.Type_chunk , -2 + dbo.udf_NthIndex(u.Type_chunk , '[' , 1
)
)AS [Learning Objectives]
, RIGHT(u.Type_chunk , 1 + LEN(u.Type_chunk
) - dbo.udf_NthIndex(u.Type_chunk , '[' , 1
)
)AS Type
FROM unpivot1 u;
如果你能夠使用正則表達式,你可以節省一些代碼。在SQL Server 2008中使用RegEx需要CLR。 This book很好地向您展示瞭如何一步一步做到這一點。該解決方案適用於每門課程的少量「類型」值。
感謝您格式化我的文章。我已經花了數小時的研究來弄清楚如何做到這一點,這讓我感到非常緊張。 billinkc:我注意到我遇到的很多帖子都被你回答了。你能幫我解決一個問題嗎? – Josh
您能否將數據至少暫時存儲在數據庫表中?例如SQL Server? –
是的,SQL Server可以將數據插入表中。 – Josh