将函数插入 SQL 语句

Inserting Function into SQL Statement

全新的功能。从其他人那里接收到函数来解决数据拉取中的 html 标签。不知道如何将此代码合并到我的查询中。

CREATE FUNCTION [dbo].[mcl_RemoveVisionMemoFormat] 
(@String NVARCHAR(MAX)) 
 RETURNS NVARCHAR(MAX) 
AS 
BEGIN 
declare @start int, 
        @end int, 
        @length int 

while charindex('<', @string) > 0 and charindex('>', @string, charindex('<', 
@string)) > 0 
begin 
    select  @start  = charindex('<', @string),  
            @end    = charindex('>', @string, charindex('<', @string)) 
    select @length = (@end - @start) + 1 

    if @length > 0 
    begin 
        select @string = stuff(@string, @start, @length, '') 
        end 
    end 

return replace(@string , '&nbsp;' , ' ') 
END

以上函数需要添加到一个基本的SELECT语句中

SELECT LD.WBS1 as [Project Number], LD.Name, LD.Comment, LD.TransDate as 
[Comment Date]
FROM LD
WHERE (((LD.Comment) Is Not Null))
ORDER BY LD.TransDate DESC;

非常感谢!

您的问题严格来说是如何在使用列作为参数的查询中使用标量函数

为此,您只需将函数添加到 select 语句并传入列。

Select [schema].[ScalarFunction](column) as [ColumnName] from [schema].table

对于您提供的查询,您只需添加函数 [dbo].[mcl_RemoveVisionMemoFormat],然后在括号

中添加列名 LD.Comment
SELECT LD.WBS1 as [Project Number], LD.Name, LD.Comment, LD.TransDate as [Comment Date], [dbo].[mcl_RemoveVisionMemoFormat](LD.Comment) as [CommentWithoutVisionMemoFormat]
FROM LD
WHERE LD.Comment IS NOT NULL
ORDER BY LD.TransDate DESC;