I am stuck with a scenario where I need to cast a particular column as BIGINT and check whether the number is not greater than X, but, the column will not always have numeric values.
I tried the following approach but it is throwing an error.
DECLARE @RowType TABLE
(
RowTypeID INT IDENTITY,
RowType VARCHAR(10)
);
INSERT @RowType VALUES('Numeric');
INSERT @RowType VALUES('NonNumeric');
DECLARE @TempTable TABLE
(
ID INT IDENTITY,
RowTypeID INT,
Value VARCHAR(10)
);
INSERT @TempTable VALUES(1, '10');
INSERT @TempTable VALUES(1, '20');
INSERT @TempTable VALUES(2, '$10'); -- Non Numeric value
-- This select throws error however ever I feel the behaviour
-- to be odd since the innser select should return only records of type 'NUMERIC'
SELECT *
FROM (SELECT T.*
FROM @TempTable T
JOIN @RowType RT
ON RT.RowTypeID = T.RowTypeID
WHERE RT.RowType = 'Numeric') A -- With this sub query I expect only records of type 'NUMERIC' to be returned to the outer select
WHERE CAST(A.Value AS BIGINT) > 10
-- Alternate approach which I can not use since
-- there are already lot of temp tables involved in procedure
--SELECT T.*
--INTO #Temp
--FROM @TempTable T
-- JOIN @RowType RT
-- ON RT.RowTypeID = T.RowTypeID
--WHERE RT.RowType = 'Numeric';
--SELECT *
--FROM #Temp
--WHERE CAST(Value AS BIGINT) > 10
--DROP TABLE #Temp;
Is this the default behaviour? Or am I missing something over here?

