I have a table with string filed for age of children that separate with ',' or '-' like this
I want to find items that contain '4' for example 4-9-14 , . i want to separate items with ',' or '-' then find '4'
I have a table with string filed for age of children that separate with ',' or '-' like this
I want to find items that contain '4' for example 4-9-14 , . i want to separate items with ',' or '-' then find '4'
You really should fix the data model. In the absence of that, like
might be your best choice. I am thinking:
where ' ' + replace(replace(ageOfChildren, ',', ' '), '-', ' ') + ' ' like '% 4 %'
This changes the delimiter to ' '
by replacing all commas and hyphens. It then looks for ' 4 '
after the replacement.
Note the following:
You should have a separate table with one row per child and a date that stores the date of birth.
If all you want to do is find rows where the AgeOfChildren column contains the character '4', this query should do it
SELECT * FROM [table] WHERE AgeOfChildren LIKE '%4%'
If you want to first seperate items with comma and dash, then find items with AgeOfChildren 4 :
SELECT * FROM [table]
WHERE AgeOfChildren NOT LIKE '%,%'
AND
AgeOfChildren NOT LIKE '%-%'
AND
AgeOfChildren LIKE '%4%'