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?

有帮助吗?

解决方案

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.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top