gpt4 book ai didi

sql-server - sql server if 语句

转载 作者:行者123 更新时间:2023-12-05 00:13:02 25 4
gpt4 key购买 nike

我确实遇到过这种情况,我正在尝试使用 if else 进行以下操作。它确实有效,但没有按预期工作。当没有为 preState 选择任何内容时,它将带来 Null I希望它为每个选择显示。你能看看它吗。谢谢大家..我的 if 有问题......“你可以复制并运行它”

       DECLARE     @returnvalue VARCHAR(50)
,@state VARCHAR(50)
,@i INT
,@postState VARCHAR(50)
,@preState VARCHAR(50)
,@Anystring VARCHAR(255)
,@state1 VARCHAR(50)
,@state2 VARCHAR(50)

SET @Anystring='IA all the states Colorado '

SELECT @state =LTRIM((RTRIM(SUBSTRING(@Anystring, LEN(@Anystring) - CHARINDEX(' ', REVERSE(@Anystring))+1 , LEN(@Anystring)))))

SET @state =REPLACE(REPLACE(@Anystring, '.', ' '), '&', ' ')

set @state1=@state


SET @i = CHARINDEX(' ',@state1 )

If (@i > 0 and SUBSTRING(@state1, 1, @i-1) IN ('IA','CO','Iowa','Colorado'))
begin
SELECT @preState = RTRIM(SUBSTRING(@state1, 1, 1)),
@state1 = LTRIM(SUBSTRING(@state1, @i+1, 999))
end

else IF (@i > 0 and SUBSTRING(@state, 1, @i-1) IN ('DC','SEA','NY'))
begin
SELECT @preState = RTRIM(SUBSTRING(@state1, 1, 2)),
@state1 = LTRIM(SUBSTRING(@state1, @i+1, 999))

end


SET @i = LEN(@state1) - CHARINDEX(' ', REVERSE(rtrim(@state1)))
IF (@i > 0 and @i < LEN(@Anystring) and SUBSTRING(@state1, @i+2, 999) IN ('IA','CO','Iowa','Colorado'))
begin
SELECT @postState = SUBSTRING(@state1, @i+2, 1),
@state1 = RTRIM(SUBSTRING(@state1, 1, @i))
end

else IF (@i > 0 and @i < len(@state) and SUBSTRING(@state1, @i+2, 999) IN ('DC','SEA','NY'))
begin
SELECT @postState = SUBSTRING(@state1, @i+2, 2),
@state1 = RTRIM(SUBSTRING(@state1, 1, @i))
end

else IF (@i > 0 and SUBSTRING(@state1, 1, @i-1) IN ('',' ',' '))
begin

SELECT @postState = '',
@state1 = LTRIM(SUBSTRING(@state1, 1, 999))
end
SET @state1=@preState+' ' + @state1+' '+@postState

set @state2=@state1


select @state2

最佳答案

如果你的意思是你从中得到 NULL

SET  @state1=@preState+' ' + @state1+' '+@postState 

然后你会想要 ISNULL 它来处理缺少前缀

SET  @state1= isnull(@preState+' ','') + @state1+' '+@postState 

@poststate 也是如此

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

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