I am creating a function to return '0' or '1' depending on the result of nested if else statements. Using MSSQL.
ALTER FUNCTION udf_check_names_starting_with_quotationmark
(@string NVARCHAR(100))
RETURNS INT
AS
BEGIN
DECLARE @condition INT
SET @string = LTRIM(@string)
SET @string = RTRIM(@string)
IF (PATINDEX('"%', @string) !=0)
BEGIN
SET @condition = 1
END
ELSE IF (PATINDEX('%"%', @string) !=0)
BEGIN
SET @condition=1
END
ELSE IF (PATINDEX('%"', @string) !=0)
BEGIN
SET @condition = 1
END
ELSE
BEGIN
SET @condition=0
END
RETURN @condition
END
Everything is working fine with this. Is there a better way to achieve this (I have tried using OR but SQL editor showing an error, not recognizing the OR).
SET @condition = (case when PATINDEX('"%', @string) !=0 or
PATINDEX('%"%', @string) !=0 or
PATINDEX('%"', @string) !=0 then 1 else 0 end)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With