Stefan_G (7/31/2013)
Since you are on SQL2008, you can forget about TRY_CAST since this is a SQL2012 feature only.On SQL2008 you could use a CASE expression to guard the expression evaluation to avoid this issue.
Like this:
UPDATE#t2
SETTTT_Factureren = 0,
TTT_FactuurStatus = 0,
TTT_FactuurNummer = 0
WHERETTT_ProjectID = 26
AND TTT_FactuurStatus = 1
AND TTT_ID NOT IN(
SELECT
CASE WHEN X.TextDescription LIKE 'Int.nr.TXT:%'
THEN CAST(SUBSTRING(CAST(X.TextDescription AS VARCHAR(100)),13,10) as int)
ELSE -1
END
FROM #t1 X
WHERE X.TextDescription LIKE 'Int.nr.TXT:%'
)
Note the use of LIKE which gives much better performance than SUBSTRING in the predicate
This method is also more efficient than casting TTT_ID to a varchar since it is much faster to compare integers than strings.
Good luck
That's pretty much how I saw it too, Stefan ๐
For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden