首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在符号和空格之间选择字符串的一部分

在符号和空格之间选择字符串的一部分
EN

Stack Overflow用户
提问于 2015-11-08 02:16:50
回答 2查看 31关注 0票数 0

我需要创建一个子查询(或视图)列,从长字符串的一部分中提取值。值将如下所示:

“招聘人员:招聘人员姓名日期:.”

我需要在“招聘人员名称”之后选择“招聘人员名称”,并以“招聘人员名称”之后的空格结尾。我知道正常化会更好,但在这种情况下,我们只有查询访问权限,而不是数据库设置访问权限。

想法被欣赏了!

EN

回答 2

Stack Overflow用户

发布于 2015-11-08 02:34:26

你可以用正则表达式来做这个。regex将允许您表示要搜索文本招聘人员,后面跟着冒号、空格和一系列字符,后面跟着空格,并且希望它提取这些字符。

表达式可能看起来有点像(未经测试的)

代码语言:javascript
复制
Recruiter: (.+) Date:

这将查找“招聘者:”,后面跟着长度为1或更长的任何字符(.)的字符串(+),该字符串将被提取(括号),后面跟着文字字符串‘Date:’。

如何在SQL中使用这一点取决于您的供应商。

票数 1
EN

Stack Overflow用户

发布于 2015-11-08 03:21:55

我会创建一个函数来提取给定键的值。你会把它当作:

代码语言:javascript
复制
        select [dbo].[GetValue]('recruiter', 
                             'aKey: the a value Recruiter: James Bond cKey: the c value')

这是“詹姆斯·邦德”

以下是功能:

代码语言:javascript
复制
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
go
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33590019

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档