One more Approach using numbers table and split string functions
declare @string varchar(max)
set @string = 'aaa,2,dqw,3,asdad,5,4'
;with cte
as
(
select * from [dbo].[SplitStrings_Numbers](@string,',')
)
select
* from cte where isnumeric(item)=1
Output:
2
3
5
4
if you are sure about no special characters in your data..You can use above,,but some times using NUMERIC tends to show some characters as numbers
SELECT
ISNUMERIC('123') as '123' --1
,ISNUMERIC('.') as '.' --Period ---1
,ISNUMERIC(',') as ',' --Comma ---1
In this case,you can use TRY_Parse available from SQL server 2012..
declare @string varchar(max)
set @string = 'aaa,2,dqw,3,asdad,5,4'
;with cte
as
(
select b.* from [dbo].[SplitStrings_Numbers](@string,',') a
cross apply
(select try_parse(a.item as int) ) b(val)
)
select
* from cte where val is not null