I have this scenario:
A table named table 1, with a column named groups.
I that column there are a range of groups which I need to split and afterwards compare with the variable myGroup
Currently I am doing this:
myGroup = 32
trueGroup = false
sql = "select * from table1 where groups like '%" & myGroup & "%'"
set rs = conn.execute(sql)
if not rs.eof
do until rs.eof
title = rs("title")
groups = rs("groups")
groupsSplitted = split(groups, ",")
for i = lbound(groupsSplitted) to ubound(groupsSplitted)
if cint(myGroup) = cint(groupsSplitted(i)) then
trueGroup = true
end if
next
if trueGroup
response.write(title)
end if
next
end if
Is it possible to do all in the SQL line? 🙂
Normalise your data.
Having multiple values in a single field (comma delimited or otherwise) is a bad anti-pattern. It destroys performance by blinding the optimiser to use of indexes, and makes queries difficult to write and maintain.
Instead, change your schema to have a 1:many relationship…
Then your query is simplified…
You can even then also enforce constraints…
– title_id foreign keyed to the table of titles
– group_id foreign keyed to the table of groups