使用SQLServer查找从特定字符到下一个空格字符的子字符串

zsbz8rwp  于 2021-08-13  发布在  Java
关注(0)|答案(2)|浏览(319)
DECLARE @c varchar(100)
SET @c = 'This is and example #COIN-XXXX data only'

所以基本上,我想要硬币之后的所有东西-到空间,即(仅x)。
我该怎么解决?

pgky5nke

pgky5nke1#

另一种方法是:

DECLARE @word varchar(max) = '#COIN-XXXX This is and example data only'
DECLARE @c varchar(100) = '#COIN1-'
DECLARE @CharIndex int = (select CHARINDEX(@c, @word))
if @CharIndex = 0
    select 'No matching word'
DECLARE @firstSpaceAfter_index int = (select CHARINDEX(' ', @word, @CharIndex))
if @firstSpaceAfter_index = 0
    set @firstSpaceAfter_index = len(@word) + 1

SELECT REPLACE(SUBSTRING(@WORD, @CharIndex, @firstSpaceAfter_index - @CharIndex),@c, '')
7cwmlq89

7cwmlq892#

一种方法是:

select left(v.str, charindex(' ', v.str) - 1)
from (select @c as str) x cross apply
     (values (stuff(x.str, 1, charindex('#COIN', x.str) + 5, ''))) v(str);

这是一把小提琴。

相关问题