如何在 SQL 服务器中的 VARCHAR(MAX) 中 select 两个已知字符串之间的可变长度字符串,其中某些列没有适用的字符串
How to select a variable length string from between two known strings in SQL Server in a VARCHAR(MAX) where some columns don't have applicable strings
使用SQL Server 2012,我需要从这个例子中得到
ColumnName
--------------------------------
Enroll to: Carol Goals are many and varied
characters that don't include desired results
Enroll to: Jan Levinson Goals will be discussed at first encounter
Enroll to: Stephon-Anderson Goals none
NULL
Enroll to: David Goals --Note uneven spaces, Need David
将列提取为:
Name
-----------
Carol
NULL
Jan Levinson
Stephon-Anderson
NULL
David
这段代码让我非常接近我正在寻找的结果,但有时会错误地修剪名称。
Select
CASE WHEN AssignedTo like '%Enroll To:%' THEN SUBSTRING(AssignedTo, CHARINDEX('%Enroll To:%', AssignedTo) + LEN('%Enroll To:%')
,CHARINDEX('Goals', AssignedTo) - CHARINDEX('%Enroll To:%', AssignedTo) + LEN('Goals'))
ELSE 'None'
END AS 'Name'
FROM
(
Select
CASE WHEN ColumnName like '%Enroll To:%' THEN SUBSTRING (ColumnName, CHARINDEX('Enroll To:', ColumnName), 40)
ELSE 'None'
END AS 'AssignedTo'
FROM TABLE ) A
感激不尽!
您可以使用 apply
和字符串函数:
select left(v.s1, charindex(' ', s1) - 1)
from t cross apply
(values (stuff(t.col, 1, 11, '')) v(s1)
这是 Gordon 答案的替代方法:
SELECT
SUBSTRING(ColumnName,
CHARINDEX(':', ColumnName) + 2,
CHARINDEX(' ', ColumnName, CHARINDEX(':', ColumnName) + 2) -
CHARINDEX(':', ColumnName) - 2) AS Name
FROM yourTable;
这产生了预期的结果,似乎可以处理目标字符串的可变长度。希望它能帮助别人。
DECLARE @pretext as NVARCHAR(100) = 'Enroll to:'
DECLARE @posttext as NVARCHAR(100) = 'Goals'
Select
,CASE When CHARINDEX(@posttext, ColumnName) - (CHARINDEX(@pretext, ColumnName) + len(@pretext)) < 0 THEN NULL
Else
SUBSTRING(ColumnName, CHARINDEX(@pretext, ColumnName) + len(@pretext)
,CHARINDEX(@posttext, ColumnName) - (CHARINDEX(@pretext, ColumnName) + len(@pretext)) )
END as betweentext
FROM TABLE
这是您要测试的 table 形式的数据:
declare @goals table (string nvarchar(255));
insert @goals values
('Enroll to: Carol Goals are many and varied characters that don''t include desired results'),
('Enroll to: Jan Levinson Goals will be discussed at first encounter'),
('Enroll to: Stephon-Anderson Goals none'),
(NULL),
('Enroll to: David Goals '), --Note uneven spaces, Need David
(' '); -- I (psw) added this
下面的代码似乎可以毫无错误地完成您想要的操作。但假设你的名字后面的句子总是以 "Goals".
开头
select *,
result =
case
when isValid = 1 then
ltrim(rtrim(
substring(string, colonPos + 1, goalsPos - colonPos - 1)
))
end
from @goals
cross apply (select
colonPos = charindex(':', string),
goalsPos = patIndex('%goals%', string)
) positions
cross apply (select
isValid =
case
when colonPos = 0 or goalsPos = 0 or colonPos > goalsPos then 0
else 1
end
) validity
使用SQL Server 2012,我需要从这个例子中得到
ColumnName
--------------------------------
Enroll to: Carol Goals are many and varied
characters that don't include desired results
Enroll to: Jan Levinson Goals will be discussed at first encounter
Enroll to: Stephon-Anderson Goals none
NULL
Enroll to: David Goals --Note uneven spaces, Need David
将列提取为:
Name
-----------
Carol
NULL
Jan Levinson
Stephon-Anderson
NULL
David
这段代码让我非常接近我正在寻找的结果,但有时会错误地修剪名称。
Select
CASE WHEN AssignedTo like '%Enroll To:%' THEN SUBSTRING(AssignedTo, CHARINDEX('%Enroll To:%', AssignedTo) + LEN('%Enroll To:%')
,CHARINDEX('Goals', AssignedTo) - CHARINDEX('%Enroll To:%', AssignedTo) + LEN('Goals'))
ELSE 'None'
END AS 'Name'
FROM
(
Select
CASE WHEN ColumnName like '%Enroll To:%' THEN SUBSTRING (ColumnName, CHARINDEX('Enroll To:', ColumnName), 40)
ELSE 'None'
END AS 'AssignedTo'
FROM TABLE ) A
感激不尽!
您可以使用 apply
和字符串函数:
select left(v.s1, charindex(' ', s1) - 1)
from t cross apply
(values (stuff(t.col, 1, 11, '')) v(s1)
这是 Gordon 答案的替代方法:
SELECT
SUBSTRING(ColumnName,
CHARINDEX(':', ColumnName) + 2,
CHARINDEX(' ', ColumnName, CHARINDEX(':', ColumnName) + 2) -
CHARINDEX(':', ColumnName) - 2) AS Name
FROM yourTable;
这产生了预期的结果,似乎可以处理目标字符串的可变长度。希望它能帮助别人。
DECLARE @pretext as NVARCHAR(100) = 'Enroll to:'
DECLARE @posttext as NVARCHAR(100) = 'Goals'
Select
,CASE When CHARINDEX(@posttext, ColumnName) - (CHARINDEX(@pretext, ColumnName) + len(@pretext)) < 0 THEN NULL
Else
SUBSTRING(ColumnName, CHARINDEX(@pretext, ColumnName) + len(@pretext)
,CHARINDEX(@posttext, ColumnName) - (CHARINDEX(@pretext, ColumnName) + len(@pretext)) )
END as betweentext
FROM TABLE
这是您要测试的 table 形式的数据:
declare @goals table (string nvarchar(255));
insert @goals values
('Enroll to: Carol Goals are many and varied characters that don''t include desired results'),
('Enroll to: Jan Levinson Goals will be discussed at first encounter'),
('Enroll to: Stephon-Anderson Goals none'),
(NULL),
('Enroll to: David Goals '), --Note uneven spaces, Need David
(' '); -- I (psw) added this
下面的代码似乎可以毫无错误地完成您想要的操作。但假设你的名字后面的句子总是以 "Goals".
开头select *,
result =
case
when isValid = 1 then
ltrim(rtrim(
substring(string, colonPos + 1, goalsPos - colonPos - 1)
))
end
from @goals
cross apply (select
colonPos = charindex(':', string),
goalsPos = patIndex('%goals%', string)
) positions
cross apply (select
isValid =
case
when colonPos = 0 or goalsPos = 0 or colonPos > goalsPos then 0
else 1
end
) validity