How to query for strings based on their length
create table dict {id, word varchar2(255)).
I store strings of varying lengths within the 'word' field. How do I query for the following cases:-
- Search all words which are 4 characters in length
- Search all words which are greater than 4 characters in length
- Search all words which have 4-8 characters (inclusive)
- Search all words which are lesser than 4 character开发者_如何学JAVAs in length
Env: h2, mysql. The data consists of english words, but would like to know the limitations if you use multibyte data (e.g japanese)
SELECT
*
FROM
dict
WHERE
LENGTH(word) = 4 /* 1. */
OR (LENGTH(word) > 4) /* 2. */
OR (LENGTH(word) >=4 AND LENGTH(word) <= 8) /* 3. */
OR (LENGTH(word) <4) /* 4. */
/* Choose the right WHERE clause, based on your case */
For multibyte data, you should use CHAR_LENGTH instead of LENGTH
You could refine the question and explain what was not clear in the documentation for string functions.
CHAR_LENGTH() docs state clearly that
Returns the length of the string str, measured in characters. A multi-byte character counts as a single character. This means that for a string containing five two-byte characters, LENGTH() returns 10, whereas CHAR_LENGTH() returns 5.
精彩评论