2017-05-12 35 views
-1

我想提取字符串,它是两个字符之间的一部分,该字符串看起来像 -提取一部分是两种字符之间,但也有多种的相同的字符

例/ EXMPL/B基本/ /例

我想移动到一个单独的列中的文本是3楼和4“/”(粗体)

+7

您正在使用哪种RDBMS? –

+0

'split_part(the_column,'/',4)' –

+0

正确答案是RDBMS特有的。在不知道使用什么RDBMS的情况下回答这个问题是纯粹的猜测。 –

回答

0

如果不知道DBMS之间的文本的第4节,我不确定是否有内置的分割功能o不是。 SQL Server 2008 R2中没有一个分裂的功能,所以我做这种方式:

declare @str varchar(max) = 'example/example/bbasic/eXaMpLe/example', @delim char(1) = '/' 

;with vars(id, _start, _end) as 
(
    select 1, cast(1 as bigint), charindex(@delim, @str) 

    union all 

    select id + 1, _end + 1, charindex(@delim, @str, _end + 1) 
    from vars 
    where _end > 0 
) 
select id, cast(substring(@str, _start, case when _end > 0 then _end - _start else len(@str) - _start + 1 end) as varchar(max)) as value 
from vars 
where id = 4 
option (MAXRECURSION 0) 

使用where id = 4手段拉,第4单元第3 /分隔符之后。此SQL返回:

id value 
--- -------- 
4 eXaMpLe 
相关问题