Avatar of gregpers
gregpers

asked on 

Setting variables in an IF statement

Trying to set either a SSN variable (if 9 characters) or an ID variable (if 6 characters) like this:

declare @SSN as varchar
declare @ID as varchar
declare @Input as varchar


set @input = 'passed in variable of type char'
set @length = len(@input)

set @SSN =  case when @length = 9 then @input else '0' end
set @ID = case when @length = 6 then @input else '0' end

I've tried this in many different fashions and am getting caught up on something elementary that I can't seem to search effectively, any help is appreciated.  Again I want @SSN to be assigned @input if char count is 9, otherwise I want @ID = @input.
Microsoft SQL Server

Avatar of undefined
Last Comment
gregpers

8/22/2022 - Mon