Good afternoon folks.
I'll preface this with "this was a hard question to ask". I'm running into conversion errors when I think I've got that part covered. Obviously, I do not.
The situation: VARCHAR field with INT data in it, plus some random garbage strings that are causing conversion issues. Here's an example of what I'm trying to do.
DECLARE @MyTABLE TABLE (
Value VARCHAR(50) NOT NULL
);
-- insert some strings
INSERT INTO @MyTABLE (Value) VALUES('400'), ('H-100'), ('H-200'), ('500'),
('600'), ('H-300');
-- conversion fails for the actual strings
SELECT *
FROM @MyTABLE m
WHERE CAST(m.Value AS INT) BETWEEN 1 AND 1000;
-- what I THOUGHT would fix it, but doesn't...
SELECT *
FROM (SELECT * FROM @MyTABLE WHERE Value NOT LIKE 'H%') X
WHERE CAST(X.Value AS INT) BETWEEN 1 AND 1000;
I realize there are other ways that I can do this, such as inserting all BUT the bad data into a temp table and querying that, but I'd like to know why my query doesn't work and what I could do to fix it.
EDIT - This is for SQL 2008 R2
Thanks in advance!