Anthony
Anthony

Reputation: 11

IF ELSE syntax error

My IF..ELSE block fails at BEGIN. I can understand how my expression is not boolean in nature, but what is causing this failure?

IF (SUBSTRING(@PARIDIN,1,1) = 'P' 
BEGIN
  SET @PARIDTEMP = SUBSTRING(LTRIM(RTRIM(@PARIDIN)),2,6))
END
BEGIN
  ELSE SET @PARIDTEMP = @PARIDIN
END

Upvotes: 1

Views: 1692

Answers (3)

ps2goat
ps2goat

Reputation: 8475

You have an extra set of parentheses in a weird place.:

IF SUBSTRING(@PARIDIN,1,1) = 'P' -- one at the beginning of this line.
 BEGIN
  SET @PARIDTEMP = SUBSTRING(LTRIM(RTRIM(@PARIDIN)),2,6) -- one at the end of this line.
 END
ELSE
 BEGIN
  SET @PARIDTEMP = @PARIDIN
 END

Upvotes: 2

Sean Lange
Sean Lange

Reputation: 33571

Taking everybody else's suggestion of fixing the condition I am surprised nobody mentioned the very strange BEGIN END blocks. They don't make sense and not sure they would even work. Since you have only a single statement for each it makes more sense to remove them.

IF (SUBSTRING(@PARIDIN,1,1) = 'P')
  SET @PARIDTEMP = SUBSTRING(LTRIM(RTRIM(@PARIDIN)),2,6))
ELSE 
    SET @PARIDTEMP = @PARIDIN

Upvotes: 0

Ross Bush
Ross Bush

Reputation: 15175

You are not closing the expression being evaluated.

Change

IF (SUBSTRING(@PARIDIN,1,1) = 'P'

To

IF (SUBSTRING(@PARIDIN,1,1) = 'P')

Upvotes: 1

Related Questions