【发布时间】:2013-05-26 21:10:06
【问题描述】:
我在 SQL 用户定义函数上收到此错误:
在上下文中指定的非布尔类型的表达式 条件是预期的,在 ')' 附近。
为此:
UPDATE LMI_Contact
SET Phone = NULL
WHERE dbo.LMI_IsSingleCharacterRepeated(Phone, '0')
可以使用以下方法创建函数的位置:
-- ***this will also find NULL and empty string values***
CREATE FUNCTION LMI_IsSingleCharacterRepeated (@string varchar(max), @char char(1))
RETURNS bit
AS
BEGIN
DECLARE @index int
DECLARE @len int
DECLARE @currentChar char(1)
SET @index = 1
SET @len= LEN(@string)
WHILE @index <= @len
BEGIN
SET @currentChar = SUBSTRING(@string, @index, 1)
IF @currentChar = @char
SET @index= @index+ 1
ELSE
RETURN 0
END
RETURN 1
END;
GO
此函数用于检查字符串是否为任何指定的单个字符,重复。
【问题讨论】:
-
警告 - 我的函数也会找到具有空值或空字符串的字段,所以真的需要更多的工作
标签: sql function boolean where-clause