【问题标题】:Inserting Function into SQL Statement在 SQL 语句中插入函数
【发布时间】:2018-04-09 12:59:10
【问题描述】:

全新的功能。从其他人那里接收函数来解决数据拉取中的 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;

非常感谢!

【问题讨论】:

  • 您使用的是哪个 dbms? (该代码是特定于产品的。)
  • 哪一列有html标签?
  • 无...我不是 dba。我只是将数据拉入 Power BI。
  • 带有HTML的列是LD.Comment
  • (((LD.Comment) Is Not Null)) 你可以多加一些括号

标签: sql-server tsql stored-functions


【解决方案1】:

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

为此,您只需将函数添加到 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;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-04-03
    • 1970-01-01
    • 2023-03-17
    • 1970-01-01
    • 1970-01-01
    • 2012-01-30
    • 1970-01-01
    相关资源
    最近更新 更多