Question

I have a column in a table in MySQL which contains ngrams, and I want to validate that each ngram is of the correct length.

For e.g. if the ngram is 'sophisticated hacking scheme' then the number of blanks should be two.

Is there anything in MySQL that would count the blanks? Or can someone suggest a quick SQL fix?

Was it helpful?

Solution

One that I've used in SQL Server, slightly adapted to using MySQL functions is:

CHAR_LENGTH(Column) - CHAR_LENGTH(REPLACE(Column,' ','')

That is - what's the length of the column, vs the length of the column if all of the spaces are removed? That should give you the number of spaces that were present.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top