我确实有这种情况,我正在尝试将 if else 用于以下情况。它确实有效,但没有按预期工作..当preState没有选择任何内容时,它会带来 Null 我希望它为每个选择显示。你能请看一下。谢谢大家..我的 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