How to select one text column, which string length >0? - mysql

Is there any function like "strlen" in mysql?

Mysql does have a length function to return the length of a string.
so you could do something like.
select * from some_table where length(some_string) > 0;

You can do it with using LENGTH keyword
SELECT * FROM table WHERE LENGTH (name) > 150;

How to select one text column, which string length >0 ?
You could compare to the empty string:
SELECT yourcolumn
FROM yourtable
WHERE yourcolumn <> ''

The following query would do the job for you.
select length(your_text_column) from sample_table where length(some_column) > 0

For anyone who comes across this answer in Google, keep this in mind:
Actually, CHAR_LENGTH() should be a better choice. For multi-byte charsets LENGTH() will give you the number of bytes the string occupies, while CHAR_LENGTH() will return the number of characters
MySQL - How to select data by string length

Related

Count values with the same length [duplicate]

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**

How to select everything that does not start with s number in MySQL

I am trying to find all values that do not start with a number
I have tried this query, but not sure if the REGEXP is correct. This seem to be returning any value the does not contains a number
SELECT * FROM table where address NOT REGEXP '[0-9]'
I think this fixed the issue
SELECT * FROM table where address NOT REGEXP '^[0-9]'
In MySQL if you try to convert a string to a number then it will start from the beginning taking the digits and convert as much as possible.
If a string does not start with a number then the result is 0.
select * from your_table
where address * 1 = 0
SQLFiddle demo
You can use explicit conversion with cast(str_column as unsigned) or implicit conversion by using a mathematical operator like * 1

How to truncate the begining character from string in mysql for a specific condition

I want to truncate the first letter of a string which have size more than 11 character.
I know I can use substring function like
SELECT SUBSTRING(name, 1, 10) from table1;
which will truncate and return me the first 10 letter. what should I do if I want to remove the character from the beginning if the string is greater than 10 character.
abcdefghijklmn ==> efghijklmn
How about RIGHT():
SELECT RIGHT(name, 10)
FROM table1;
Demo: SQL Fiddle
RIGHT() returns a specified number of characters from the right side of a string.
If you want to apply any function only in certain situations, a CASE statement can be used.
create table table1(name char(25));
insert into table1 values('abcdefghijklmn');
select right(name,10) from table1;
RIGHT() is the function you need to use.

Query in MySQL for string fields with a specific length

How can I make a MySQL query asking for all the string fields in a column that have a particular length?
Thanks!
Use LENGTH() for checking length in bytes:
SELECT str FROM sometable WHERE LENGTH(str) = 5;
Or CHAR_LENGTH() for checking length in number of characters (useful for multi-byte strings):
SELECT str FROM sometable WHERE CHAR_LENGTH(str) = 5;
select *
from myTable
where length(myColumn) = 5

MySQL SELECT WHERE 'a' IN (`field`)

I know it is not an appropriate technique to have a structure of MySQL table as such, but I have to work with such. The problem is, that the field in table has value with comma seperated integers, like "1,3,5,7,10" and I want the query to return rows, in which field has a to the query passed number in it, like:
SELECT * FROM `table` WHERE '5' IN (`field_in_table`)
However, it does not work, if, in this case, '5' is not the first number in the field.
Any advises, how to solve that?
Thanks in advance,
Regards,
Jonas
Have a look at
FIND_IN_SET
Returns a value in the range of 1 to N
if the string str is in the string
list strlist consisting of N
substrings. A string list is a string
composed of substrings separated by
“,” characters. If the first argument
is a constant string and the second is
a column of type SET, the
FIND_IN_SET() function is optimized to
use bit arithmetic. Returns 0 if str
is not in strlist or if strlist is the
empty string.
You could use WHERE field_in_table LIKE '%5%' instead.
Of course, the problem would be, '1,59,98' would return as wel.
SELECT * FROM table WHERE field_in_table LIKE '%5'");
should work
You could try
SELECT *
FROM table
WHERE '%,5,%' LIKE field_in_table OR
'%,5' LIKE field_in_table OR
'5,%' LIKE field_in_table;
A better approach might be to use regular expressions, a subject on which I am not an authority.
SELECT *
FROM table
WHERE FIELD LIKE '%,5,%' OR
FIELD LIKE '5,%' OR
FIELD LIKE '%,5'