Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running SQL user defined function that returns boolean, in where clause

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.

like image 601
Chris Halcrow Avatar asked May 31 '13 05:05

Chris Halcrow


People also ask

How do I return a true or false function in SQL?

CREATE OR REPLACE FUNCTION fun_tiene_cita (id_paciente number, fecha_cita date) return number is begin if (exists(select id_paciente from citas where id_paciente = 500 and fecha_cita = 03/03/2020)) then return 'true'; else return 'false'; end if; END fun_tiene_cita; Obviously, it isn't working, so, what could I do?

Can we use functions in where clause of an SP?

You can not use Stored Procedure in where clause but can use User Defined Function in where clause.

How can you set boolean true SQL?

You can update boolean value using UPDATE command. If you use the BOOLEAN data type, MySQL internally convert it into tinyint(1). It can takes true or false literal in which true indicates 1 to tinyint(1) and false indicates 0 to tinyint(1).


1 Answers

You must use comparison operators against functions even if the return type is bit.

UPDATE LMI_Contact SET Phone = NULL WHERE dbo.LMI_IsSingleCharacterRepeated(Phone, '0') = 1 
like image 178
Thomas Avatar answered Sep 23 '22 03:09

Thomas