我已經在這里完成了大量與此相關的查詢SUBSTRING
,CHARINDEX
但我找不到能回答我的查詢的查詢。
我正在提取一段長文本的一個部分,但它回傳的 48 個字符太多了。例如,在結果的第一行中,長度應該是 44 個字符,而不是它給出的 92 個字符。我將查詢的各個部分拆分為最后 3 列以獲得起始位置和長度,但是當我將它們放在一起時它不能正常作業。我究竟做錯了什么?
我的代碼:(我希望更新為消耗品)
IF OBJECT_ID('tempdb.dbo.#WQuery', 'U') IS NOT NULL
DROP TABLE #WQuery
CREATE TABLE #WQuery
(
IncidentID VARCHAR(100),
Description VARCHAR(250),
)
INSERT INTO #WQuery
(IncidentID, Description)
VALUES
('B209BBA0-9039-ED11-81AD-0050569FE3BD','<b>Please indicate your company''s export status:</b><br />New to export (less than 1 years experience)<br /><br /><b>What is the destination market for your goods/services?</b><br />United Kingdom<br /><br />'),
('13A75070-0F38-ED11-81AD-0050569FE3BD','<b>Please indicate your company''s export status:</b><br />Novice exporter (1-3 years experience)<br /><br /><b>What is the destination market for your goods/services?</b><br />Kazakhstan <br /><br />'),
('D2926CA8-EB28-ED11-81AC-0050569FE3BD','<b>Please indicate your company''s export status:</b><br />New to export (less than 1 years experience)<br /><br /><b>What is the destination market for your goods/services?</b><br />Zambia (central Africa)<br/'),
('7226B826-DF24-ED11-81AC-0050569FE3BD','<b>Please indicate your company''s export status:</b><br />Experienced exporter (3 years experience in multiple markets)<br /><br /><b>What is the destination market for your goods/services?</b><br />Spain<br'),
('E636692C-3C22-ED11-81AC-0050569FE3BD','<b>Please indicate your company''s export status:</b><br />New to export (less than 1 years experience)<br /><br /><b>What is the destination market for your goods/services?</b><br />India<br /><br />'),
('C13937A0-EF16-ED11-81AC-0050569FE3BD','<b>Please indicate your company''s export status:</b><br />New to export (less than 1 years experience)<br /><br /><b>What is the destination market for your goods/services?</b><br />Rotterdam<br /><br /><b>')
;
select
i.incidentid
, i.description
, SUBSTRING(i.description,
CHARINDEX('export status:</b><br />', i.description) LEN('export status:</b><br />'),
CHARINDEX('<br /><br /><b>What is the destination market',i.description) - (CHARINDEX('export status:</b><br />', i.description)-LEN('export status:</b><br />')) ) As ExportStatus
--substring(string,start,length)--
--length is the number of characters to extract - must be positive--
--split the substring above to test the values--
,CHARINDEX('export status:</b><br />', i.description) LEN('export status:</b><br />') as start
,CHARINDEX('<br /><br /><b>What is the destination market',i.description) as secondQStart
,CHARINDEX('<br /><br /><b>What is the destination market',i.description) - CHARINDEX('export status:</b><br />', i.description)-LEN('export status:</b><br />') as length
--charindex(substring,string,start)--
from #WQuery i
DROP TABLE #WQuery;
第 3 列(出口狀態)的預期結果應為:
但是我得到了這個:
當我將“長度”列中的代碼添加到 SUBSTRING 查詢的引數中時,它給出了一個錯誤。
訊息 537,級別 16,狀態 3,行 3
傳遞給 LEFT 或 SUBSTRING 函式的長度引數無效。
當我在 SUBSTRING 的那部分周圍添加括號時,查詢運行,但正如您所見,它沒有回傳相同數量的字符和最后一列中的“長度”。
我很難理解為什么 SUBSTRING 查詢中的長度引數與“長度”列中的相同代碼的作業方式不同。而且我也不明白為什么代碼需要在 SUBSTRING 查詢的引數中使用括號才能作業或引發錯誤。
uj5u.com熱心網友回復:
您的文本幾乎是有效的 XHTML。在某些情況下,您只需要修復行尾,我認為這是一個復制粘貼錯誤。
例如,<br />Spain<br
應該是<br />Spain<br />
,Rotterdam<br /><br /><b>
應該是Rotterdam<br /><br /><b />
。
現在您可以使用 XQuery 來獲取正確的文本,這似乎是第一個沒有被任何節點包圍的文本。
SELECT ExportStatus = CAST(wq.Description AS xml).value('(/text())[1]', 'nvarchar(max)')
FROM #WQuery wq
db<>小提琴
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/514587.html
標籤:tsqlsql-server-2008-r2子串字符索引