gpt4 book ai didi

sql - 嵌套 if else 语句

转载 作者:行者123 更新时间:2023-12-02 16:05:22 30 4
gpt4 key购买 nike

我正在创建一个函数,根据嵌套 if else 语句的结果返回“0”或“1”。使用 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

一切正常。有没有更好的方法来实现这一点(我尝试过使用 OR,但 SQL 编辑器显示错误,无法识别 OR)。

最佳答案

SET @condition = (case when PATINDEX('"%', @string) !=0 or 
PATINDEX('%"%', @string) !=0 or
PATINDEX('%"', @string) !=0 then 1 else 0 end)

关于sql - 嵌套 if else 语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15900532/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com