0

Can anyone advice what is wrong here?

I got multiple syntax errors.

IF (('05:00' < CAST(t.Scan_In_Prod AS TIME)) AND (CAST(t.Scan_In_Prod AS TIME) < '08:00'))
BEGIN
    (CASE WHEN (CAST(t.Scan_In_Prod as TIME) < CAST(t.Scan_Out_Prod AS TIME)) AND (CAST(t.Scan_Out_Prod as TIME) < '19:00')
             THEN 'Early Out'
          WHEN ('07:00' < CAST(t.Scan_In_Prod AS TIME)) AND (CAST(t.Scan_In_Prod AS TIME) < CAST(t.Scan_In_Prod AS TIME))
             THEN 'Late In'
     END)
END

Errors are shown below:

Msg 156, Level 15, State 1, Line 25
Incorrect syntax near the keyword 'if'.

Msg 156, Level 15, State 1, Line 29
Incorrect syntax near the keyword 'case'.

Msg 156, Level 15, State 1, Line 42
Incorrect syntax near the keyword 'case'.

Msg 102, Level 15, State 1, Line 107
Incorrect syntax near 't'.

3
  • You have a CASE statement sitting there in isolation with no query. This is the problem. Commented Dec 28, 2017 at 3:58
  • But the CASE has the experssion --> (CAST(t.Scan_In_Prod as TIME) < CAST(t.Scan_Out_Prod as TIME)) and (CAST(t.Scan_Out_Prod as TIME) < '19:00') Commented Dec 28, 2017 at 3:59
  • Can advise how to amend the whole query? Thanks. Commented Dec 28, 2017 at 3:59

2 Answers 2

1

Try prefacing the inner CASE expression with SELECT:

IF '05:00' < CAST(t.Scan_In_Prod AS TIME) AND
   '08:00' > CAST(t.Scan_In_Prod AS TIME)
BEGIN
    SELECT
        CASE WHEN CAST(t.Scan_In_Prod AS TIME) < CAST(t.Scan_Out_Prod AS TIME) AND
                  CAST(t.Scan_Out_Prod AS TIME) < '19:00'
             THEN 'Early Out'
             WHEN '07:00' < CAST(t.Scan_In_Prod AS TIME) AND
                  CAST(t.Scan_In_Prod as TIME) < CAST(t.Scan_In_Prod AS TIME)
             THEN 'Late In' END
END
Sign up to request clarification or add additional context in comments.

Comments

0

NO paranthese, you should use select i change only

if (('05:00' < CAST(t.Scan_In_Prod as TIME)) and  (CAST(t.Scan_In_Prod as TIME) < '08:00'))
 begin   
 SELECT case when (CAST(t.Scan_In_Prod as TIME) < CAST(t.Scan_Out_Prod as TIME)) and (CAST(t.Scan_Out_Prod as TIME) < '19:00')
        then 'Early Out'
          when  ('07:00' < CAST(t.Scan_In_Prod as TIME)) and (CAST(t.Scan_In_Prod as TIME) < CAST(t.Scan_In_Prod as TIME))
        then 'Late In'
 end

end

1 Comment

I am glad user548682 . can you check my answer is useful?

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.