1

I need to filter some results from a query where the field must have more than a given length. I know that doesn't work, but it would be something like this:

SELECT * FROM MyTable WHERE COUNT(description) > 50

Is that doable or will I have to filter that on PHP(in my case) later?

BenMorel
  • 34,448
  • 50
  • 182
  • 322
Caio Favero
  • 2,196
  • 3
  • 17
  • 18
  • `I know that doesn't work`<-- you should throw this thought out when you do something (at-least before asking at SO). – bansi Dec 05 '13 at 11:57
  • By the way, an aggregate function cannot be used in WHERE clause. You could probably use - select * from my_table where length(my_col)>50; – Aditya Kakirde Dec 05 '13 at 12:21

4 Answers4

4

Given you're using MySQL, you're probably looking for LENGTH().

For standard SQL, the function is called LEN().

If you are dealing with UTF-8, you will have to use CHAR_LENGTH() as LENGTH() measures the length in bytes while CHAR_LENGTH() will correctly measure the length in characters.

tyteen4a03
  • 1,812
  • 24
  • 45
0
SELECT * FROM MyTable WHERE LENGTH(description) < 50
Mike
  • 2,391
  • 6
  • 33
  • 72
0

Use this query:

SELECT * FROM MyTable WHERE LENGTH(description) > 50

Read more: LENGTH function in mysql.

Reference: MySQL - How to select data by string length

Community
  • 1
  • 1
Code Lღver
  • 15,573
  • 16
  • 56
  • 75
0

Try This

SELECT * FROM MyTable WHERE LENGTH(description) > 50
naveen goyal
  • 4,571
  • 2
  • 16
  • 26