1

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
1
  • 1
    ALL CAPS IS SOOO MUCH FUN! But you should stop that right now - it's really hard to read, really annoying - and it's considered SHOUTING at people which is rude and unwelcome. Please don't do it. Commented Nov 13, 2014 at 21:55

3 Answers 3

2

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
Sign up to request clarification or add additional context in comments.

1 Comment

Due to what @Sean Lange mentioned, I moved the ELSE outside of the BEGIN and END block.
1

You are not closing the expression being evaluated.

Change

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

To

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

Comments

0

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

3 Comments

BEGIN and END are not strange. They are there for a reason. They allow you to execute multiple statements inside of the IF or ELSE blocks. IMO, it is better to have this construct before others modify the code, so we don't end up with unexpected results (thought and ELSE block would cause a syntax error without a valid IF block before it. Though I do see the incorrectly placed ELSE as a problem.
I fully understand the point, I was stating the structure of the way the OP had them was quite strange. The else was inside the BEGIN. Sorry you misunderstood my comment I should have been more clear.
Thank you Sean, that was indeed very helpful

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.