In MySQL, how can I order my query by character count?
Try using the LENGTH function:
SELECT * FROM table ORDER BY LENGTH(myField);
Depending on what you're doing, you might want to use CHAR_LENGTH instead:
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.
If you don't know what that means, you probably want LENGTH.
Related
I have a table with a column having values like:
AB123
AB209
ABQ52
AB18C
I would like to extract rows whose last three characters are numbers. How can I do this?
The original table is more complicated, and I tried the "WHERE" clause with "AB___", which returned the above to me.
You can use a combination of SUBSTRING and REGEXP like this:
SELECT yourcolumn FROM yourtable WHERE SUBSTRING(yourcolumn, -3) REGEXP '^[0-9]+$';
The SUBSTRING part will cut the last 3 characters of the column's value and the REGEXP condition will check whether this substring is numeric.
SELECT * FROM table ORDER BY string_length(column);
Is there a MySQL function to do this (of course instead of string_length)?
You are looking for CHAR_LENGTH() to get the number of characters in a string.
For multi-byte charsets LENGTH() will give you the number of bytes the string occupies, while CHAR_LENGTH() will return the number of characters.
SELECT * FROM table
ORDER BY LENGTH(column);
Documentation on the LENGTH() function, as well as all the other string functions, is available here.
Having a look at MySQL documentation for the string functions, we can also use CHAR_LENGTH() and CHARACTER_LENGTH() as well.
In my case I get data using mobile number length greater than 10 digits using the below query
SELECT * FROM table_name WHERE CHAR_LENGTH(mobile) > 10;
The function that I use to find the length of the string is length, used as follows:
SELECT * FROM table ORDER BY length(column);
I used this sentences to filter
SELECT table.field1, table.field2 FROM table WHERE length(field) > 10;
you can change 10 for other number that you want to filter.
select * from *tablename* where 1 having length(*fieldname*)=*fieldlength*
Example if you want to select from customer the entry's with a name shorter then 2 chars.
select * from customer where 1 **having length(name)<2**
my table has a column with comma-separated (and eventually a space, too) numbers; those numbers can have from five to twelve digits.
9645811, 9646011,9645911, 9646111
or
41031, 41027, 559645811, 5501006009
I need to select the rows with that column containing a number STARTING with given digits. In the above examples, only the first has to be selected. What I've tried so far:
SELECT myfield FROM mytable
WHERE myfield REGEXP ('(^|[,\s]+)(96458[\d]*)([,\s]*|$)');
However the query returns no results. I'd like to select only the first row, where there is a number STARTING with 96458.
Any help would be appreciated :)
You need to use a starting word boundary [[:<:]]:
SELECT myfield FROM mytable WHERE myfield REGEXP ('[[:<:]]96458');
See the MySQL regex syntax for more details.
[[:<:]], [[:>:]]
These markers stand for word boundaries. They match the beginning and end of words, respectively.
See this SQL fiddle.
SELECT IF('y' = 'i', 1, 2 ) -> 1 why?
Can I change encoding or somethint to get it right? and how to order strings like irish and yes
now field and table encoded in utf8_lithuanian_ci
so how to order list with these characters?
You can compare/order those strings using BINARY operator -
SELECT * FROM table ORDER BY BINARY column;
From the reference - The BINARY operator casts the string following it to a binary string. This is an easy way to force a column comparison to be done byte by byte rather than character by character.
Alphabetic sort is performed with respect of the collation, so you have to find which is better for you.
http://dev.mysql.com/doc/refman/5.0/en/charset-general.html
Ordering works the same for strings as it does for integers. It performs an alphabetic sort.
SELECT * FROM table ORDER BY column ASC
Is there an efficient way to limit the number of characters for a mysql query lookup? So, if a text column field had 18000 characters, but I only need to query 500 characters, is there a way to mysql_query("select text_column from random_table limit_char_count 500")?
How about using the SUBSTRING() function?
Below example selects 4 characters starting from 1st position
SELECT SUBSTRING(text_column,1,4)
FROM random_table
WHERE something = something else
EDIT - edited based on - for all forms of SUBSTRING(), the position of the first character in the string from which the substring is to be extracted is reckoned as 1.