所以,如果我有这个字符串:这是为了告知我们组织的#Department#和#Employees#。我只想要这两个字#Dectate#和#Employees#
答案 0 :(得分:1)
你是否愿意为此付出代价
declare @string varchar(100) ='This is to inform #Department# and #Employees# of out.............'
,@string_1st_part varchar(100)
declare @table table (col varchar(100) )
while (CHARINDEX (' #',@string) >0)
begin
set @string =substring (@string,CHARINDEX (' #',@string)+2,len(@string) )
set @string_1st_part =substring (@string,1,CHARINDEX ('# ',@string))
insert into @table
select '#'+@string_1st_part
end
select *from @table
答案 1 :(得分:0)
首先在数据库中创建拆分功能
CREATE FUNCTION [dbo].[Split]
(
@String NVARCHAR(4000),
@Delimiter NCHAR(1)
)
RETURNS TABLE
AS
RETURN
(
WITH Split(stpos,endpos)
AS(
SELECT 0 AS stpos, CHARINDEX(@Delimiter,@String) AS endpos
UNION ALL
SELECT endpos+1, CHARINDEX(@Delimiter,@String,endpos+1)
FROM Split
WHERE endpos > 0
)
SELECT 'Id' = ROW_NUMBER() OVER (ORDER BY (SELECT 1)),
'Data' = SUBSTRING(@String,stpos,COALESCE(NULLIF(endpos,0),LEN(@String)+1)-stpos)
FROM Split
)
GO
然后使用以下代码行获得适当的输出
declare @str varchar(100) ='This is to inform #Department# and #Employees# of out.............'
SELECT data from [dbo].[Split] (@str,'#')
WHERE ID%2=0