t-sql 替换文本字段

发布于 2024-07-09 02:32:50 字数 295 浏览 8 评论 0原文

我遇到了一个经典问题,需要对 sql 2000 数据库中的文本字段进行字符串替换。 这可以是对整个列的更新,也可以是对单个字段的更新,我并不挑剔。

我找到了一些如何使用 updatetext 来实现它的示例,但它们往往在存储过程中,有谁知道包装在函数中的类似东西,以便我可以像通常使用 Replace() 一样使用它。 对于任何不知道的人来说,Replace() 函数的问题是它不支持文本字段。

编辑:我意识到我可能可以摆脱 varchar(8000) 因此将字段交换为这种类型,从而解决了问题。 我从未找到真正的解决方案。

I have hit a classic problem of needing to do a string replace on a text field in an sql 2000 database. This could either be an update over a whole column or a single field I'm not fussy.

I have found a few examples of how to use updatetext to achieve it but they tend to be in stored procedures, does anyone know of a similar thing that is wrapped into a function so I can use it like I would usually use Replace(). The problem with the Replace() function for anyone who isn't aware is that it doesn't support text fields.

Edit: I realised I could probably get away with varchar(8000) so have swapped the fields to this type which fixes the issue. I never found a true solution.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

筱果果 2024-07-16 02:32:50

以下是使用 REPLACE 函数更新包含文本列的表的示例查询。 希望这对您有用。

UPDATE <Table> set textcolumn=
REPLACE(SUBSTRING(textcolumn,1,DATALENGTH(textcolumn)),'findtext','replacetext') 
WHERE <Condition>

Here is the sample query to update table with text column using REPLACE function. Hope this is useful for you.

UPDATE <Table> set textcolumn=
REPLACE(SUBSTRING(textcolumn,1,DATALENGTH(textcolumn)),'findtext','replacetext') 
WHERE <Condition>
时光病人 2024-07-16 02:32:50

恐怕您无法在函数中执行此操作

当您尝试声明如下函数时:

create function dbo.textReplace(
@inText as text)
returns text
as 
begin
    return 'a' -- just dummy code
end

您将收到以下错误:

The text data type is invalid for return values.

换句话说,您无法为文本数据类型编写与 REPLACE 函数等效的简单函数

I am afraid you cannot do it within a function

When you try to declare a function like:

create function dbo.textReplace(
@inText as text)
returns text
as 
begin
    return 'a' -- just dummy code
end

You will get the following error:

The text data type is invalid for return values.

In other words you could not write a simple equivalent of REPLACE function for the text data type

好听的两个字的网名 2024-07-16 02:32:50

这是我针对此场景的代码片段:

DECLARE @oldtext    varchar(1000)
DECLARE @newtext    varchar(1000)
DECLARE @textlen    int
DECLARE @ptr        binary(16)
DECLARE @pos        int
DECLARE @id         uniqueidentifier

SET @oldtext = 'oldtext'
SET @newtext = 'newtext'
SET @textlen = LEN(@oldtext)

DECLARE mycursor CURSOR LOCAL FAST_FORWARD
FOR
    SELECT  [UniqueID]
            ,TEXTPTR([Text])
            ,CHARINDEX(@oldtext, [Text]) - 1
    FROM    [dbo].[myTable] 
    WHERE   [Text] LIKE '%' + @oldtext +'%'

OPEN mycursor

FETCH NEXT FROM mycursor into @id, @ptr, @pos

WHILE @@fetch_status = 0
BEGIN   
    UPDATETEXT [dbo].[myTable].Text @ptr @pos @textlen @newtext

    FETCH NEXT FROM mycursor into @id, @ptr, @pos   
END

CLOSE mycursor
DEALLOCATE mycursor

This is my code snippet for this scenario:

DECLARE @oldtext    varchar(1000)
DECLARE @newtext    varchar(1000)
DECLARE @textlen    int
DECLARE @ptr        binary(16)
DECLARE @pos        int
DECLARE @id         uniqueidentifier

SET @oldtext = 'oldtext'
SET @newtext = 'newtext'
SET @textlen = LEN(@oldtext)

DECLARE mycursor CURSOR LOCAL FAST_FORWARD
FOR
    SELECT  [UniqueID]
            ,TEXTPTR([Text])
            ,CHARINDEX(@oldtext, [Text]) - 1
    FROM    [dbo].[myTable] 
    WHERE   [Text] LIKE '%' + @oldtext +'%'

OPEN mycursor

FETCH NEXT FROM mycursor into @id, @ptr, @pos

WHILE @@fetch_status = 0
BEGIN   
    UPDATETEXT [dbo].[myTable].Text @ptr @pos @textlen @newtext

    FETCH NEXT FROM mycursor into @id, @ptr, @pos   
END

CLOSE mycursor
DEALLOCATE mycursor
ヤ经典坏疍 2024-07-16 02:32:50

如果要替换 ntext 字段,则必须将文本字段转换为 varchar(8000) 或 nvarchar(4000)。

MyField = REPLACE(CAST(MyField as VARCHAR(4000)), "string1", "string2")

当然,只有当您可以保证字段中的内容长度小于等于 4000/8000 个字符时,这才有效。

You would have to cast the text field to a varchar(8000) or nvarchar(4000) if you are replacing over an ntext field.

MyField = REPLACE(CAST(MyField as VARCHAR(4000)), "string1", "string2")

This ofcourse will only work if you can guarantee the content in the field is <= 4000/8000 characters in length.

瀞厅☆埖开 2024-07-16 02:32:50

您还可以使用 SUBSTRING() 函数,该函数在传递文本值时返回 varchar。

例如:

MyVarchar = SUBSTRING(myTextField, 1, DATALENGTH(myTextField))

如果您要填充特定长度的 varchar,则可以截断以适合:

MyVarchar100 = SUBSTRING(myTextField, 1, 100)

You can also use the SUBSTRING() function, which returns a varchar when passed a text value.

For instance:

MyVarchar = SUBSTRING(myTextField, 1, DATALENGTH(myTextField))

If you're populating a varchar with a specific length, you can truncate to fit:

MyVarchar100 = SUBSTRING(myTextField, 1, 100)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文