我有一個包含多個問題和答案的欄位。我需要將答案分別提取到一列中。文本示例:

抱歉,由于文字不斷消失,我不得不添加為圖片。
我需要提取黃色和綠色突出顯示的第一個實體(不包括突出顯示的部分)之間的文本作為 select 子句中的第一行,然后提取黃色和綠色突出顯示之間的第二個實體作為 select 中的第二行條款等等等。有 5 個問題(粉紅色和藍色突出顯示之間)和 5 個答案(黃色和綠色突出顯示之間)。
我使用黃色和綠色突出顯示的文本作為書擋嘗試了下面的代碼,但我得到了與下面相同的錯誤訊息。
然后我使用問題作為第一個書擋嘗試了以下代碼:
SELECT distinct subjectidname
, title
, i.description
, SUBSTRING(i.description, CHARINDEX('<b>Please indicate your company''s export status:</b><br />', i.description),
CHARINDEX('<br /><br />',i.description) -
CHARINDEX('<b>Please indicate your company''s export status:</b><br />', i.description) Len('<br /><br />'))
from FilteredIncident i
這兩項努力都導致了一條錯誤訊息:
訊息 537,級別 16,狀態 3,第 2 行 傳遞給 LEFT 或 SUBSTRING 函式的長度引數無效。
而且它也不考慮第 2、3、4 和 5 個實體。從包含單行文本的描述框中提取 5 個答案的最佳方法是什么?
uj5u.com熱心網友回復:
從一個字串拆分器開始,它可以拆分一個字串并為每一行回傳一個索引:
CREATE FUNCTION [dbo].[DelimitedSplit8K]
--===== Define I/O parameters
(@pString VARCHAR(8000), @pDelimiter VARCHAR(16))
--WARNING!!! DO NOT USE MAX DATA-TYPES HERE! IT WILL KILL PERFORMANCE!
RETURNS TABLE WITH SCHEMABINDING AS
RETURN
--===== "Inline" CTE Driven "Tally Table" produces values from 1 up to 10,000...
-- enough to cover VARCHAR(8000)
WITH E1(N) AS (
SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL
SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL
SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1
), --10E 1 or 10 rows
E2(N) AS (SELECT 1 FROM E1 a, E1 b), --10E 2 or 100 rows
E4(N) AS (SELECT 1 FROM E2 a, E2 b), --10E 4 or 10,000 rows max
cteTally(N) AS (--==== This provides the "base" CTE and limits the number of rows right up front
-- for both a performance gain and prevention of accidental "overruns"
SELECT TOP (ISNULL(DATALENGTH(@pString),0)) ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) FROM E4
),
cteStart(N1) AS (--==== This returns N 1 (starting position of each "element" just once for each delimiter)
SELECT 1 UNION ALL
SELECT t.N Len( @pDelimiter ) FROM cteTally t WHERE SUBSTRING(@pString,t.N, Len( @pDelimiter ) ) = @pDelimiter
),
cteLen(N1,L1) AS(--==== Return start and length (for use in substring)
SELECT s.N1,
ISNULL(NULLIF(CHARINDEX(@pDelimiter,@pString,s.N1),0)-s.N1 ,8000)
FROM cteStart s
)
--===== Do the actual split. The ISNULL/NULLIF combo handles the length for the final element when no delimiter is found.
SELECT ItemNumber = ROW_NUMBER() OVER(ORDER BY l.N1),
Item = SUBSTRING(@pString, l.N1, l.L1)
FROM cteLen l;
(感謝 Jeff Moden 多年來成功的字串拆分。)
然后選擇要拆分的正確子字串:
declare @QandA as NVarChar(1000) = '<b>Q1:</b><br />A1<br /><br /><b>Q2:</b><br />A2<br /><br /><b>Q3:</b><br />A3<br /><br /><b>Q4:</b><br />A4<br /><br />';
-- A single split gets Q/A pairs:
select ItemNumber, Item
from dbo.DelimitedSplit8K( @QandA, '<br /><br />' )
order by ItemNumber;
-- A second split gets Q's and A's:
with QAPairs as (
select ItemNumber as QuestionNumber, Item as QA
from dbo.DelimitedSplit8K( @QandA, '<br /><br />' ) )
select QuestionNumber, QA, ItemNumber, Item, case when ItemNumber % 2 = 1 then 'Q' else 'A' end as 'Q/A'
from QAPairs cross apply
dbo.DelimitedSplit8K( QA, '<br />' );
資料庫小提琴。
這應該是一個好的開始。有一些清理作業要做,例如,有一個虛假的空 Q/A 對,因為字串以 a 結尾'<br /><br />',作為分隔符,必須意味著每一側都有一個 Q/A 對。
此示例從表中檢索資料 a 將每一行分解為其組成問題和答案:
-- Sample data.
declare @QandAs as Table ( QandAId Int Identity, QandA NVarChar(1000) );
insert into @QandAs ( QandA ) values
( '<b>Q1a:</b><br />A1a<br /><br /><b>Q2a:</b><br />A2a<br /><br /><b>Q3a:</b><br />A3a<br /><br /><b>Q4a:</b><br />A4a<br /><br />' ),
( '<b>Q1b:</b><br />A1b<br /><br /><b>Q2b:</b><br />A2b<br /><br /><b>Q3b:</b><br />A3b<br /><br /><b>Q4b:</b><br />A4b<br /><br />' );
select * from @QandAs;
-- A single split gets Q/A pairs:
with QAPairs as (
select QandAId, ItemNumber, Item, Row_Number() over ( partition by QandAId order by ItemNumber desc ) as RN
from @QandAs cross apply
dbo.DelimitedSplit8K( QandA, '<br /><br />' ) )
select QandAId, ItemNumber, Item, RN
from QAPairs
where RN > 1 -- Eliminate the extraneaous empty Q/A pair at the end of the string.
order by QandAId, ItemNumber;
-- A second split gets Q's and A's:
with QAPairs as (
select QandAId, ItemNumber as QuestionNumber, Item as QA, Row_Number() over ( partition by QandAId order by ItemNumber desc ) as RN
from @QandAs cross apply
dbo.DelimitedSplit8K( QandA, '<br /><br />' ) )
select QandAId, QuestionNumber, QA, ItemNumber, Item, case when ItemNumber % 2 = 1 then 'Q' else 'A' end as 'Q/A'
from QAPairs cross apply
dbo.DelimitedSplit8K( QA, '<br />' )
where RN > 1 -- Eliminate the extraneaous empty Q/A pair at the end of the string.
order by QandAId, QuestionNumber, ItemNumber;
資料庫小提琴。
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/514592.html
下一篇:如何為每場比賽回傳一個新列
