我需要创建一个子查询(或视图)列,从长字符串的一部分中提取值。值将如下所示:
“招聘人员:招聘人员姓名日期:.”
我需要在“招聘人员名称”之后选择“招聘人员名称”,并以“招聘人员名称”之后的空格结尾。我知道正常化会更好,但在这种情况下,我们只有查询访问权限,而不是数据库设置访问权限。
想法被欣赏了!
发布于 2015-11-08 02:34:26
你可以用正则表达式来做这个。regex将允许您表示要搜索文本招聘人员,后面跟着冒号、空格和一系列字符,后面跟着空格,并且希望它提取这些字符。
表达式可能看起来有点像(未经测试的)
Recruiter: (.+) Date:这将查找“招聘者:”,后面跟着长度为1或更长的任何字符(.)的字符串(+),该字符串将被提取(括号),后面跟着文字字符串‘Date:’。
如何在SQL中使用这一点取决于您的供应商。
发布于 2015-11-08 03:21:55
我会创建一个函数来提取给定键的值。你会把它当作:
select [dbo].[GetValue]('recruiter',
'aKey: the a value Recruiter: James Bond cKey: the c value')这是“詹姆斯·邦德”
以下是功能:
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
create function [dbo].[GetValue](@Key varchar(50), @Line varchar(max))
returns varchar(max)
as
begin
declare @posStart int, @posEnd int
select @posStart=charindex(@Key, @Line) -- the start of the key
if(@posStart = 0)
return '' -- key not found
set @posStart = @posStart + len(@Key) + 1 -- the start of the value
select @line = substring(@line, @posStart, 1000) -- start @Line at the value
select @posEnd=charindex(':', @line) -- find the next key
if(@posEnd > 0)
begin
-- shorten @line to next ":"
select @line = substring(@line, 0, @posEnd)
-- take off everything after the value
select @posEnd = charindex(' ', reverse(@line));
if(@posEnd > 0)
select @line = substring(@line, 0, len(@line) - @posEnd + 1)
end
return rtrim(ltrim(@line))
end
gohttps://stackoverflow.com/questions/33590019
复制相似问题