I have following table "Managers" (simplified):
ID, int
Name, nvarchar(100)
In a stored procedure that has one argument ("Search", type nvarchar), I want to select every row where
At the moment, my select in the stored procedure looks something like this:
SELECT ID, Name FROM Managers WHERE
(ISNUMERIC(@Search) = 1 AND [ID] = CAST(@Search AS INT)) Or
Contains([Name], @Search)
If I call the stored procedure with @Search = 1321 (example), the select works.
But if I have a @Search - parameter that is not numeric (example "HES"), I get the following error: Conversion failed when converting the nvarchar value 'HES' to data type int.
How can I fix this?
Thanks in advance
Raphi
SELECT ID, Name FROM Managers
WHERE [ID] = CASE
WHEN ISNUMERIC(@Search) = 1 AND @Search NOT LIKE '%.%' THEN CAST(@Search AS INT)
ELSE -1
END
OR Contains([Name], @Search)