Home > Software engineering >  Filtering a column for exactly one word and three words in sql server
Filtering a column for exactly one word and three words in sql server

Time:06-28

I'm trying to scan one column and return only rows that have one or three words. I have tried using the below query snip but it didn't return anything. Any suggestion?

SELECT
      Column_1
      Column_2
FROM Table
WHERE 
      Column_1 LIKE '% %' OR Column_1 LIKE '% % %'

Data Examples:

Column_1 would contain the following:

Check;ACH;Wire or Check;Wire or ACH

CodePudding user response:

If your words are separated by ; and you can trust that , one way is by counting them :

select * 
from tablename 
where 
  (datalength(Column_1) - datalength(replace(Column_1,';',''))) / datalength(';') in (1,2)
  • Related