在特定列上执行功能

时间:2014-10-28 17:16:39

标签: sql-server sql-function

我有以下功能将文本转换为正确的大小写:

USE [Messaging]
GO
/****** Object:  UserDefinedFunction [dbo].[ProperCase]    Script Date: 10/28/2014 11:42:05 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER  Function [dbo].[ProperCase](@Text as varchar(8000))
 returns varchar(8000)
 as
 begin
    declare @Reset bit;
    declare @Ret varchar(8000);
    declare @i int;
    declare @c char(1);

    select @Reset = 1, @i=1, @Ret = '';

    while (@i <= len(@Text))
          select @c= substring(@Text,@i,1),
                @Ret = @Ret + case when @Reset=1 then UPPER(@c) else LOWER(@c) end,
                @Reset = case when @c like '[a-zA-Z]' then 0 else 1 end,
                @i = @i +1
    return @Ret
 end

该功能有效。如果我运行select dbo.propercase ('DOE, JOHN'),输出将是“Doe,John”。这正是我想要的。问题是,我想在邮箱表的MailBoxName字段上运行此函数。我该怎么做?

3 个答案:

答案 0 :(得分:0)

您可以使用更新语句执行此操作。这将对邮箱表上的MailBoxName字段运行dbo.propercase函数 - 这将把MailBoxNames更新为原样。

UPDATE Mailbox
SET MailBoxName = dbo.propercase(MailBoxName)

答案 1 :(得分:0)

只需将其放入选择语句

即可
select dbo.propercase (MailBoxName) as Proper
from Mailbox

为什么这是一个谜?

答案 2 :(得分:0)

SELECT *, dbo.propercase(MailBoxName) AS ProperCaseMailBoxName
FROM Mailbox