27

I'm getting this error on a SQL user defined function:

An expression of non-boolean type specified in a context where a condition is expected, near ')'.

For this:

UPDATE LMI_Contact
SET Phone = NULL
WHERE dbo.LMI_IsSingleCharacterRepeated(Phone, '0')

where the function can be created using:

-- ***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

This function is for checking if a string is any specified single character, repeated.

4

3 に答える 3

30

戻り値の型が であっても、関数に対して比較演算子を使用する必要がありますbit

UPDATE LMI_Contact
SET Phone = NULL
WHERE dbo.LMI_IsSingleCharacterRepeated(Phone, '0') = 1
于 2013-05-31T06:06:42.730 に答える
8

これを試して

CREATE FUNCTION LMI_IsSingleCharacterRepeated (@str varchar(max), @char char(1))
RETURNS BIT
AS 
BEGIN
    DECLARE @indx int
    DECLARE @len int
    DECLARE @currentChar char(1)
    SET @indx = 1
    SET @len= LEN(@str)

    WHILE @indx <= @len
    BEGIN
        SET @currentChar = SUBSTRING(@str, @indx, 1)
        IF @currentChar = @char
            SET @indx= @indx+ 1
        ELSE
            RETURN 0
    END
    RETURN 1
END;
GO
于 2013-05-31T06:00:56.263 に答える
5

クエリの where 句を次のように変更する必要があります。

UPDATE LMI_Contact
SET Phone = NULL
WHERE dbo.LMI_IsSingleCharacterRepeated(Phone, '0') = 1
于 2013-05-31T06:09:41.850 に答える