SUBSTRING function mysql - mysql

So I have a task to show all teachers who have their first 3 letters of name and surname the same.
I know I should use substring but I cant figure this out....
select SUBSTRING(name_surname,1,3)
from teacher
where
This is what I did so far

First three letters of any value? SUBSTR(). As you know:
SUBSTR(surname, 1, 3)
Comparing two values of these for a WHERE filter?
WHERE SUBSTR(surname, 1, 3) = SUBSTR(given, 1, 3)

Related

how to randomize column data and mask data using mysql

DISCLAIMER: NONE OF THESE VALUES ARE TRUE/REAL, ITS JUST A PRACTICE ASSIGNMENT
how to randomize the last 6 digits of the DBS account and obscuring the first 4 digits of the NRIC number with x using mysql. All values were keyed in manually and do not relate to each other.
Current
Desired Result
Just use substring operations:
SELECT
CONCAT('XXXX', SUBSTRING(NRIC, 5, 4)) AS NRIC,
Name,
Contact,
Salary,
CONCAT(SUBSTRING(DBS_Account, 1, 3), '-XXXXX-X') AS DBS_Account
FROM yourTable;
mysql rand function - https://dev.mysql.com/doc/refman/5.7/en/mathematical-functions.html#function_round
using substring and concat
SELECT CONCAT(SUBSTRING(RAND(),3,5),'-',SUBSTRING(RAND(),3,1))
will give something like 40033-8
then concat with substring
set #a ='038-12645-6';
SELECT CONCAT(substring_index(#a,'-',1),'-',SUBSTRING(RAND(),3,5),'-',SUBSTRING(RAND(),3,1));
'038-63475-5'

Selecting double vs single letter at end of string in MySQL

I am trying to write a MySQL select statement where I am trying to select values based on the last letter or letters of a string. The problem is... these values have double letters at the end of the string and I am not able to differentiate between them when getting results.
For example... I have the following 2 values in a table
1. Mens 3's AA
2. Mens 3's A
The query I am currently using returns both values when I only want to return #2 above. Here is the query:
SELECT divisions.div_id, divisions.div_lname
FROM divisions
WHERE LEFT(divisions.div_lname,1) = "M"
AND divisions.div_lname LIKE '%3%'
AND RIGHT(divisions.div_lname,1) = 'A'
ORDER BY divisions.div_nop, divisions.div_order
I really need to understand the best approach for selecting 1 but not the other when I have values that contain duplicate letters at the end of the string. Is there a regex approach that would work?
try using SUBSTRING_INDEX() like this, it'll return the last chunk after the space..so it won't return rows that have 'AA'..and only return row with 'A'
SELECT divisions.div_id, divisions.div_lname
FROM divisions
WHERE LEFT(divisions.div_lname,1) = "M"
AND divisions.div_lname LIKE '%3%'
AND SUBSTRING_INDEX(divisions.div_lname,' ',-1) = 'A'
ORDER BY divisions.div_nop, divisions.div_order

fetch first occurrence of numbers from a table column

I want to fetch first occurrence of numbers from a table column.
To illustrate:
For column value 'C 62/3,Industrial Area, Phase 2', I should get '62/3'
For column value 'B-5 dummy, (dummy)', I should get 'B-5'
For column value '21 dummy - 2, dummy' I should get '21'.
I tried:
select address,SUBSTRING(address, 1, LOCATE(' ', address) - 1) AS str
from items;
Well, I'm not a MySQL expert but I think you have to use a library like mysql-udf-regexp. Using this you can get the first digit-containing sub string with REGEXP_SUBSTR:
SELECT
address,
REGEXP_SUBSTR(address, '[[^:space:]]*[[:digit:]]+[[^:space:]]*', 1, 1) AS str
FROM items;
This is untested code but should give you an idea how to proceed. It means that you get any positive number of digits ([[:digit:]]+) followed and succeeded by any number of non-white spaces ([[^:space:]]*); starting search from first character and return first occurrence (,1 ,1).

mysql - SELECT FROM .. TO

Is it possible in MySQL to select rows for a certain range of items?
For example when I want to select all items in where the first letter of the NAME is between the B and T, alphabetically.
I know I can make this is PHP aswell, but it would save me a bit of time if this is possible in MySQL...
Is it possible, and if so, how?
The ideal situation would be something like this:
$sql="SELECT * FROM paths FROM name=name1 TO name=name6"; //which would select name1, 2, 3, 4, 5, 6.
Using BETWEEN will basically get you there, but you need to use one letter past where you want to end. Experiment until you get the result you desire.
SELECT * FROM paths WHERE UPPER(name) BETWEEN 'B' AND 'U';
The idea here is that everything beginning with a 'T' will sort alphabetically before anything beginning with a 'U'. You need to convert it to upper-case via UPPER() so you don't run up against potential collation problems.
So your results could be like:
B,
Bill
Bob
Jane
Tommy
Travis
But Uwe (He's German) would be excluded.
You can use BETWEEN like:
SELECT * FROM paths WHERE name BETWEEN 'B' AND 'U'

MySQL : Problem in Updating a column for condition

I have one phone field column which contains phone numbers like that
'123456789'
'123-456-789'
etc
means it contain 9 digit number or number + hyphen.
I want to make a SQL query which updates all records in 'xxx-xxx-xxx' format.
I have made few attempts but cannot get exact solution.
Please any one help me.
Thanks in advance.....
use something like
UPDATE mytable SET phone =
CONCAT(SUBSTRING(phone, 1, 3),'-',SUBSTRING(phone, 4, 3),'-',SUBSTRING(phone, 7, 3))
Also to only get the rows that are missing hyphens you would say WHERE phone not like '%-%'
Close. You'd first need to test if the string contained a '-' before adding more, but that's the right track.
UPDATE mytable
SET phone = CONCAT(SUBSTRING(phone, 1, 3),'-',SUBSTRING(phone, 4, 3),'-',SUBSTRING(phone, 7, 4))
where INSTR( phone, '-' ) = 0;