I have a stored procedure where input is a comma separated string say '12341,34567,12446,12997' and it is not sure that the input string always carries numerical data. It may be '12341,34as67,12$46,1we97' so I need to validate them and use only the valid data in query.
Say my query is (Where the column OrderCode is int type)
select * from dbo.DataCollector where OrderCode in (12341,34567,12446,12997)
or only the valid data if other are invalid
select * from dbo.DataCollector where OrderCode in (12341)
For such situation what would be a good solution.
One way that works also in SQl-Server 2005 would be to create a split-function, then you can use
ISNUMERIC
to check if it's a number:Demo
Your complete query:
Here is the split-function which i use:
Edit according to the comment of Damien that
ISNUMERIC
has it's issues. You can use this function to check if it's a real integer:Here is another example with damien's "bad" input which contains
£
and0d0
:Demo