MySQL Substring Grab all but last three characters - mysql

Basically, I am trying to find the inverse of this command: substring(term, -3).

Try this:
SELECT SubStr(myColumn, 1, LENGTH(myColumn) - 3)
FROM MyTable
or
SELECT LEFT(myColumn, LENGTH(myColumn) - 3)
FROM MyTable

This should do it: SELECT SUBSTRING(term FROM 1 FOR LENGTH(term)-3)

If you need it to match in a where comparison you could use
WHERE somefield LIKE 'term___'

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%'

SQL - can you select everything where a certain string ends with a few characters?

Can you do a "SELECT * FROM TABLE WHERE attribute has last character which is 'A' or 'B'"
I see, that everyone who answered here suggest LIKE.
However, the following would be faster than LIKE, so I suggest you use it.
SELECT *
FROM t1
WHERE substring(attribute, -1)= 'A'
OR substring(attribute, -1)= 'B';
SQLFiddle
You can try:
SELECT * FROM TABLE WHERE attribute LIKE '%A' OR attribute LIKE '%B'
Try
SELECT * FROM TABLE WHERE attribute LIKE '%A' or attribute LIKE '%B'
Try this query:
SELECT * FROM TABLE1 WHERE attribute LIKE '%A' OR attribute LIKE '%B';
You can also use Mysql's RIGHT() function
SELECT
*
FROM
`table`
WHERE RIGHT(attribute, 1) = 'A'
OR RIGHT(attribute, 1) = 'B'
Small Fiddle Demo
Yes, it's possible. For instance you could:
SELECT * FROM table WHERE attribute LIKE '%[AB]'
Which would select all rows in table with field 'attribute' ending in either 'A' or 'B'

How to select record in msql which starts with a name and followed with digits

I have a requirement to retrieve the records like "Name19","Name21","Name56", these kind of records in Mysql.Please help how to get it.
SELECT * FROM YourTable WHERE YourColumn regexp '^Name[0-9]+'
Try This
SELECT * FROM <NameTable> WHERE <Name> LIKE '%[0-9]'

Select from MYSQL where the first four characters are

I have a database, in one of the fields occasionally I get an entry which starts off as mail.domainname.com
Is it possible using mysql and php to select only the rows from the field hostname where the first 4 characters = 'mail'?
One way to do it is to use LIKE
SELECT * FROM tablename WHERE hostname LIKE 'mail%'
Another is to use SUBSTR()
SELECT * FROM tablename WHERE SUBSTR(hostname, 1, 4) ='mail'
Case sensitive:
SELECT * FROM tbl WHERE hostname LIKE BINARY 'mail%'
Case insensitive:
SELECT * FROM tbl WHERE hostname LIKE 'mail%'
Yes, you can use SQL LIKE operator: http://www.w3schools.com/sql/sql_like.asp
Simply use:
SELECT * FROM your_table WHERE SUBSTR(field_name, 1, 4) = 'mail'

How do you select from mysql where last character in a string = x?

I'd like to select rows from the database where the last character in the mov_id column equals to 1 and 2.
How would the query look like?
SELECT * FROM `myTable` WHERE `mov_id` LIKE '%1' OR `mov_id` LIKE '%2'
the % character is a wildcard which matches anything (like * in many other places)
If mov_id is a numeric value (TINYINT, INT, etc...) then you should use a numeric operator. For instance, use the modulo operator to keep the last digit
SELECT * FROM mytable
WHERE (mov_id MOD 10) IN (1, 2)
If mov_id is a string, you can use LIKE or SUBSTRING(). SUBSTRING() will be slightly faster.
SELECT * FROM mytable
WHERE SUBSTRING(mov_id, -1) IN ('1', '2')
If your table is big or that query is frequently run, you should definitely consider adding a column to your table, in which you would store mov_id's last digit/character, and index that column.
Try this way too:
SELECT field1
FROM table
WHERE RIGHT(field1, 1) = 'x'
it displays the fields that has last a value of x.
SELECT *
FROM Table
WHERE RIGHT(Column_name, 1) IN ('x')
if you want to match two character just replace 1 by 2.
In general:
RIGHT(COLUMN_NAME, NO_OF_CHARACTER_YOU WANT_TO_MATCH_FROM_LAST)
And if you want to match the starting char just use LEFT instead of RIGHT
You could also do something like:
select
your, fields, go, here
from table
where
substring(mov_id, (char_length(move_id) - 1)) = x
SELECT * FROM table WHERE mov_id REGEXP '1$' OR mov_id REGEXP '2$'