A query to check the first 2 chars of a string - mysql

I would like to know if it's possible to make a query to check the first two chars of a string?
For example, I have these entries in my table:
toto
topo
poto
I want to get the entries when the two first chars are to.

As simple as this:
SELECT *
FROM TABLE
WHERE `name` LIKE "to%"
to% means that the word should start by to and can be followed by other character

Try this:
SELECT * FROM myTable WHERE RowName LIKE 'to%'

select fieldName
from tableName
where fieldName like 'to%'

if you need to check the first 2 chars of a string but return all results..
SELECT fieldname,
if(left(fieldname,2)='XX',1,0) as XCheck
FROM tablename

Related

Retrieve any name starting with letters 'Ai' in SQL

All I want to do is retrieve the names of the people who start with "ma".
Eg Matt, Mathers, Mac.
First, two letters have to be 'ma'.
Have a look at string comparisons functions.
SELECT *
FROM YOUR_TABLE
WHERE NAME LIKE 'MA%';
SELECT *
FROM YOUR_TABLE
WHERE NAME LIKE 'MA%'
There can be spaces at the beginning of the name just like ' Matthew'. This would fix it:
SELECT *
FROM YOUR_TABLE
WHERE ltrim(NAME) LIKE 'MA%'

Query to select data from mysql with specific condition

In MySQL we can use this code to select rows with ID numbers (or anything else) between a list:
SELECT * FROM TABLENAME WHERE prophrases IN (1,2,3,4,5,6)
Thats OK! but if we need to search a number in a Field's value, what we can do?
For example, I have a table with a field, named 'prophases' and I saved data like this:
rowid / prophases
1 / 1,2,3,4,5,6,7,8
2 / 6,5,2,7,9,2
now, i need to check if a number like 6 is in prophrases in row #1 or not!
what can i do for that?
something like this but in correct form!
SELECT * FROM TABLENAME WHERE 6 IN prophrases
you should just use FIND_IN_SET()
SELECT rowid
FROM TABLENAME
WHERE FIND_IN_SET('6', prophrases)
This will work if you are only dealing with single digits
select * from tablename where prophrases like '%6%'
This will work if you are going to have number with more than one digit and there are no spaces between commas.
select * from tablename where prophrases like '%,6,%' or prophrases like '6,%' or prophrases like '%,6'
You need to use a like statement
SELECT * FROM TABLENAME WHERE prophrases LIKE '%6%'
However if you have multiple digit numbers that could cause some unexpected results, so you might have to amend it like
SELECT * FROM TABLENAME
WHERE prophrases LIKE '%,6,%'
OR prophrases LIKE '6,%'
OR prophrases LIKE '%,6'
The first part matches 6 in between commas, the second one matches a 6 at the beginning followed by a comma, the third matches a comma followed by a six a the end.
Storing data like that is not the best way of doing it. You are probably better off having another table that has a one-to-many relationship.

mysql search string and numbers like #654

I have a quick question how can I search #some number in mysql> For example
Select * from table where field contains #numbers?
where #numbers represent any number(example: #123, #345, ...) which has more then four characters. How should I do it correctly?
EDIT: I get what I need with this
Select * from table where field REGEXP '^#+[0-9]'
Is that ok? And I also need to be at least three characters long
If you need the numbers to be at least 3 digits long, try:
SELECT * FROM table WHERE field RLIKE '#[0-9]{3,}'
If I have good understood you that code should solve your problem : SELCET * FROM table WHERE field LIKE '%#%s'
SELECT * FROM table WHERE field RLIKE '#^[0-9]'
SELECT * FROM table WHERE fields REGEXP '^#[0-9]{3}'
If i understand you..
SELECT *
FROM table
WHERE field LIKE '%#[0-9][0-9][0-9]%'

MySQL select using wildcard (but wildcard in field)

I have a mysql 5 table with a char field holding
DOG
DOUG
CAT
MOUSE
Now I want to SELECT on this field, finding any rows where that field exist within a string, like "DOGGY". (This is opposite of how you normally use a wildcard). So I want a select something like:
SELECT FROM TABLE WHERE FIELD IS SUBSTRING OF "DOGGY"
Is this possible?
select * from mytable where 'doggy' like concat('%',mycol,'%')
You should be able to use LOCATE() to achieve this:
SELECT * FROM MyTable WHERE LOCATE(MyField, 'DOGGY') != 0;

How to find certain Hex values and Char() Values in a MySQL SELECT

Anyone know the syntax to search a MySQL table column by hex value or CHAR() value? Do I have to use a LIKE or IN()?
Something like:
SELECT * FROM `myWordTable` WHERE `definition` LIKE 0xE28098;
OR
SELECT * FROM `myWordTable` WHERE `definition` LIKE CHAR(128);
Thanks!
The second one is close.
Try this:
SELECT *
FROM `myWordTable`
WHERE `definition`
LIKE concat('%',CHAR(128),'%');
In the first example consider pattern match '%' == 0x25
SELECT *
FROM `myWordTable`
WHERE `definition`
LIKE 0x25E2809825;
You need to UNHEX() values you want to match on.
For example, finding all accented e's that were corrupted into 0xC3A9:
SELECT id, FirstName
FROM Person
WHERE FirstName
RLIKE UNHEX('c3a9');