1

I am trying to query a table in mysql based on the length of a string in a specific column. I know mysql has a function called LENGTH(), but that returns the length of the string. I want to be able to pull data based on the result of the LENGTH() function.

Example:

SELECT * table WHERE LENGTH(word) = 6

of course that does not work. I read through http://dev.mysql.com/doc/refman/5.0/en/string-functions.html#function%5Flength but could not find anything to help me.

yes I could make something in PhP to accomplish this, but I would like to do it at the query level.

Any help?

0

2 Answers 2

4

Try:

SELECT *
    FROM table
    WHERE LENGTH(RTRIM(word)) = 6
Sign up to request clarification or add additional context in comments.

2 Comments

I would recommend using the CHAR_LENGTH() function instead of LENGTH(), that way you're counting characters instead of bytes.
worked perfect, ultimatly I am making a query statement that will pull results based on "String Length", "Possible Characters", and not "Including Characters" in words. I have the first 2 so far
0

I believe you wanted to use query SELECT * FROM tableName WHERE LENGTH(word) = 6; (assuming that the word is name of column in tableName).

This is very unfortunate solution on large tables, you should create new column and use UPDATE tableName SET wordLength = LENGTH( word).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.