【问题标题】:UDF to insert a text string in to each row of a tableUDF 在表格的每一行中插入一个文本字符串
【发布时间】:2014-08-28 23:46:30
【问题描述】:

我一直在寻找答案,但我似乎找不到任何指向正确方向的东西。

我需要创建一个 UDF,它将提取文本字符串的每个单词并返回一个表格,其中字符串的每个单词位于单独的行中。

UDF 只能接受一个变量“@mytext”。

我们可以假设文本字符串由一个空格分隔,并且可能包含逗号或句点。

例如,“不要担心失败,担心你甚至不尝试就错过的机会。”将需要返回一个表格,其中每个单词位于列的单独行上,不存在逗号或句点。

我认为文本字符串需要用一个通用值分隔,该值可用于分隔每个单词以进行插入,但我可能完全错了。

对此的任何帮助将不胜感激!

根据我到目前为止所说的,这是我不太确定如何继续的完整代码

create function [dbo].[textConverter]
(
    @mytext nvarchar(max)
)
returns @text_string table
    (
        word nvarchar
    )
as
begin

set @mytext = replace(@mytext, 'what needs to be changed', 'what it needs to be changed too')

--insert string to table

end

编辑

我检查了几个链接并发现了更多关于此的信息,我现在得到了这段代码。但是它退出并出现错误。文章中使用的示例我在插入中找到了已使用数字的代码,所以这可能是问题所在??

create function [dbo].[textConverter]
(
    @mytext varchar(max)
)
returns @text_string table
    (
        word nvarchar
    )
as
begin

--Change string to be seperated by commas
set @mytext = replace(@mytext, ' ', ',')
set @mytext = replace(@mytext, '.',',')

--Eliminate double commas
set @mytext = replace(@mytext, ',,', ',')

declare @name nvarchar(255)
declare @pos int

while CHARINDEX(',', @mytext) > 0
begin
select @pos = CHARINDEX(',', @mytext)
select @name = SUBSTRING(@mytext, 1, @pos-1)

insert into @text_string
select @name

select @mytext = SUBSTRING(@mytext, @pos+1, LEN(@mytext)-@pos)
end

insert into @text_string
select @mytext

return
end

--To use function
select * from dbo.textConverter('don’t worry about failures, worry about the chances you miss when you don’t even try.')

【问题讨论】:

标签: sql sql-server tsql user-defined-functions


【解决方案1】:

看下面的答案,它不是完整的形状,但可以开发成用户定义的功能。

Declare @Sentence Varchar(max) = 'don’t worry about failures, worry about the chances you miss when you don’t even try.'
Set     @Sentence = Replace(Replace(Replace(Replace(@Sentence,',',' '),'.',' '),'  ',' '),'   ',' ')


Declare @e int = (Select Len(@Sentence) - Len(Replace(@Sentence,' ','')))
Declare @s int = 1
Declare @Result Table(id int identity(1,1),Words varchar(max))

--Select @s,@e
While @s <= @e
begin
    Insert into @Result
    Select Left(@Sentence,Charindex(' ',@Sentence,1)-1)

    Set @Sentence = Substring(@Sentence,Charindex(' ',@Sentence,1) + 1,Len(@Sentence) )

Set @s = @s + 1
End

Insert into @Result
Select @Sentence

Select * from @Result

结果

----+-----------
id  |Words
----+-----------
1   |don’t
2   |worry
3   |about
4   |failures  
5   |worry
6   |about
7   |the
8   |chances
9   |you
10  |miss
11  |when
12  |you
13  |don’t
14  |even
15  |try 
----+-----------

【讨论】:

    【解决方案2】:

    我改编了http://sqlperformance.com/2012/07/t-sql-queries/split-strings 中的一些代码,因为我的情况意味着无法将分隔符指定为输入。我只能在输入上使用,那就是文本字符串。因此,以下内容对我有用:

    create function [dbo].[textConverter]
    ( 
        @string nvarchar(max)
    ) 
    returns @output table(splitdata nvarchar(max) 
    ) 
    begin 
    
    --Change string to be seperated by commas
    set @string = replace(@string, ' ', ',')
    set @string = replace(@string, '.',',')
    
    --Eliminate double commas
    set @string = replace(@string, ',,', ',')
    
        declare @start int, @end int
        select @start = 1, @end = charindex(',',@string)
    
        while @start < len(@string) + 1 
        begin
            if @end = 0
                set @end = len(@string) + 1
    
            insert into @output (splitdata)
            values(substring(@string, @start, @end - @start)) 
            set @start = @end + 1
            set @end = charindex(',', @string, @start)
    
        end
        return
    end
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-05-19
      • 2020-03-04
      • 1970-01-01
      • 1970-01-01
      • 2017-10-14
      • 2017-04-17
      • 2017-03-25
      • 2018-02-06
      相关资源
      最近更新 更多