Task

Select certain elements from an Array into a new Array in a generic way.



To demonstrate, select all even numbers from an Array.

As an option, give a second solution which filters destructively,
by modifying the original Array rather than creating a new Array.




--Create the original array (table #nos) with numbers from 1 to 10
CREATE TABLE #nos (v INT)
DECLARE @n INT SET @n=1
while @n<=10 BEGIN INSERT INTO #nos VALUES (@n) SET @n=@n+1 END
 
--Select the subset that are even into the new array (table #evens)
SELECT v INTO #evens FROM #nos WHERE v % 2 = 0
 
-- Show #evens
SELECT * FROM #evens
 
-- Clean up so you can edit and repeat:
DROP TABLE #nos
DROP TABLE #evens