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?
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.
SELECT * FROM MyTable WHERE LENGTH(description) < 50
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
Try This
SELECT * FROM MyTable WHERE LENGTH(description) > 50
Related
How do I select rows within a column where the string length equals 5 characters?
I am aware of length() and char_length() functions, but they seem to only be for sorting of data query results.
Any help is very appreciated.
SELECT * FROM yourtable WHERE CHAR_LENGTH(yourcolumn)=5
I have a column(char) with values between A and Z
I only want to select the records where the char is >= 'C'
Can anyone help me with this?
I tried >= 'C' but this didn't work. Also I couldn't find anything about this on the internet. So I thought it's a good question to ask.
You can use the ascii value for comparison.
select * from tablename where ascii(colname) >= ascii('C')
here is another method.
SELECT SUBSTRING_INDEX(YourColumn,'c',-1) FROM Yourtable;
Strings can be compared in MySQL with regular comparison operators, so this should work:
SELECT * FROM table WHERE col >= 'C'
Do note that the exact sort order (mainly case sensitivity) for strings depends on your characterset collation. Maybe that is the reason why it didn't work for you.
You can also use ASCII() function, which returns the character value of a single character, and compare those:
SELECT * FROM table WHERE ASCII(col) >= ASCII('C')
Note that this only works for single byte characters. For multi byte characters you must use ORD() instead of ASCII().
Yet another way is to use STRCMP() which compares two strings (again, using the sort order of your characterset collation) and returns 0 if the strings are the same, -1 if the first argument is smaller than the second, and 1 otherwise.
SELECT * FROM table WHERE STRCMP(col, 'C') >= 0
Values in my table have a number of decimal points:
20.828292
21.9292992
...
I need to query the table to find results that match but to only 1 decimal place.
So where 20.8 would match the 20.828292.
Can I do this with SQL? How?
you can do it like
select * from table where TRUNCATE(attribute,1)=20.8
MySQL supports lots of comparison operators, like >= and <, which should be combined to get you the range you're looking for.
You can't do it by just comparing
select * from table where atribute = 20.8
In your case do a query like
select * from table where atribute >= 20.75 and atribute < 20.85
That should do it
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?
Try:
SELECT *
FROM table
WHERE LENGTH(RTRIM(word)) = 6
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).
I have a column of type varchar that stores many different numbers. Say for example there are 3 rows: 17.95, 199.95 and 139.95.How can i sort that field as numbers in mysql
Quickest, simplest? use * 1
select *
from tbl
order by number_as_char * 1
The other reasons for using * 1 are that it can
survive some horrendous mishaps with underflow (reduced decimal precision when choosing what to cast to)
works (and ignores) columns of purely non-numeric data
strips numeric portions of alphanumeric data, such as 123A, 124A, 125A
If you need to sort a char column containing text AND numbers then you can do this.
tbl contains: 2,10,a,c,d,b,4,3
select * from tbl order by number_as_char * 1 asc, number_as_char asc
expected output: 2,3,4,10,a,b,c,d
If you don't add the second order by argument only numbers will be sorted - text actually gets ignored.
Use a CAST or a CONVERT function.
This approach is helpful when sorting text as numbers:
SELECT `my_field`
FROM `my_table`
ORDER BY `my_field` + 0;
Found the solution on http://crodrigues.com/trick-mysql-order-string-as-number/.
Pad the string with leading zeroes:
ORDER BY LPAD(`column`,<max length of string>,"0")
If you really have to you can do this if your source data is compatible:
SELECT column FROM table ORDER BY CAST(column AS DECIMAL(10,2))
It's not going to be very fast for large data sets though. If you can you should change the schema to use DECIMAL in the first place though. Then it can be properly indexed for better performance.