Select first word - mysql query - mysql

i need to select only the first name (that is the first word) of the user.
For example, Andy Jones, only select Andy
Any idea?
thanks

Take a look at substring_index.
select substring_index(field, " ", 1) ....

If I understand, your FirstName and LastName are in one column. You can achieve this using user defined functions.
Example:
SELECT SPLIT_STR(name, ' ', 1) as firstname FROM users;
Read following post for more options:
Split value from one field to two

For something like this you would normally put the first and last name in their own columns. If you cannot, then you can get the index of the first space, then substring(0,index of the first space). I would really recommend though that you split that into two columns, if you can.

Related

Cannot check if a field contains also number

I'm trying to check if a field in a specific table contains also number, in particular I have a record that have the field name which contains this value: Besëlidhja Lezhë vs. Tërbuni Pukë 1 - 1, so I'm trying to get also all the rows of that table that contains a number inside the field name. I tried:
SELECT * FROM `venue` where `name` like '%[0-9]%'
but this will return an empty result, any idea?
This should tell you if name contains any digit (not tested)
SELECT * FROM venue WHERE name REGEXP '[0-9]'
You can try using a Regular Expression that filters for names in your name column with numeric characters . For example:
SELECT * FROM DATA WHERE name REGEXP '[a-z]...[0-9]';
mySQL allows you to use regular expression as a filter !
This should select out for names like Tërbuni Pukë 1 - 1. If you want to practice regular expressions this is a great website to test whether you have the right regex. https://regex101.com/
Hope this helps !
I believe this should work:
SELECT *
FROM venue
WHERE name like '%0%' or name like '%1%' or name like '%2%' or name like '%3%'
and so on til you get to 9. I hope this helps

SQL query to display the length and first 3 characters of ename column in emp table

just as the question can we do something to get the length and first 3 characters of the employee name of one column
Please do not mark as answered or duplicate
i have the test tomorrow Advance SQL so I am trying to solve some imp question..
Please answer the problem
thanks again
Hi Shanu, You can use LEN() or LENGTH()(in case of oracle sql) function to get the length of a column.
SELECT LEN(column_name) FROM table_name;
And you can use SUBSTRING or SUBSTR() function go get first three characters of a column.
SUBSTRING( string, start_position, length );
SELECT SUBSTRING( column_name, 1, 3 ) FROM table_name;
To get both together use concatenation operator,
SELECT LEN(column_name)||SUBSTRING( column_name, 1, 3 ) FROM table_name;
Hope you got what you need. Any issues, feel free to ask
We can use SUBSTRING or SUBSTR() function, go get first three characters of a column.
And then try this particular query:
SELECT SUBSTRING(ename,1,3)
FROM emp;
Select len(ename) as Column_Length, left(ename,3) first_three_char from employee; ---------need to code your query. Should not use test format, will be confusing
You can also use substring function instead of left. Query will look like
Select len(ename) as Column_Length,substring(ename,1,3) first_three_char from employee;
SELECT LEN(EMPLOYEE_NAME),LEFT(EMPLOYEE_NAME,3) FROM EMPLOYEE_TABLE;

Query MySQL field for LIKE string

I have a field called 'areasCovered' in a MySQL database, which contains a string list of postcodes.
There are 2 rows that have similar data e.g:
Row 1: 'B*,PO*,WA*'
Row 2: 'BB*, SO*, DE*'
Note - The strings are not in any particular order and could change depending on the user
Now, if I was to use a query like:
SELECT * FROM technicians WHERE areasCovered LIKE '%B*%'
I'd like it to return JUST Row 1. However, it's returning Row 2 aswell, because of the BB* in the string.
How could I prevent it from doing this?
The key to using like in this case is to include delimiters, so you can look for delimited values:
SELECT *
FROM technicians
WHERE concat(', ', areasCovered, ', ') LIKE '%, B*, %'
In MySQL, you can also use find_in_set(), but the space can cause you problems so you need to get rid of it:
SELECT *
FROM technicians
WHERE find_in_set('B', replace(areasCovered, ', ', ',') > 0
Finally, though, you should not be storing these types of lists as strings. You should be storing them in a separate table, a junction table, with one row per technician and per area covered. That makes these types of queries easier to express and they have better performance.
You are searching wild cards at the start as well as end.
You need only at end.
SELECT * FROM technicians WHERE areasCovered LIKE 'B*%'
Reference:
Normally I hate REGEXP. But ho hum:
SELECT * FROM technicians
WHERE concat(",",replace(areasCovered,", ",",")) regexp ',B{1}\\*';
To explain a bit:
Get rid of the pesky space:
select replace("B*,PO*,WA*",", ",",");
Bolt a comma on the front
select concat(",",replace("B*,PO*,WA*",", ",","));
Use a REGEX to match "comma B once followed by an asterix":
select concat(",",replace("B*,PO*,WA*",", ",",")) regexp ',B{1}\\*';
I could not check it on my machine, but it's should work:
SELECT * FROM technicians WHERE areasCovered <> replace(areaCovered,',B*','whatever')
In case the 'B*' does not exist, the areasCovered will be equal to replace(areaCovered,',B*','whatever'), and it will reject that row.
In case the 'B*' exists, the areCovered will NOT be eqaul to replace(areaCovered,',B*','whatever'), and it will accept that row.
You can Do it the way Programming Student suggested
SELECT * FROM technicians WHERE areasCovered LIKE 'B*%'
Or you can also use limit on query
SELECT * FROM technicians WHERE areasCovered LIKE '%B*%' LIMIT 1
%B*% contains % on each side which makes it to return all the rows where value contains B* at any position of the text however your requirement is to find all the rows which contains values starting with B* so following query should do the work.
SELECT * FROM technicians WHERE areasCovered LIKE 'B*%'

MySQL Select Query - Get only first 10 characters of a value

Ok, so here is the issue.
I have a table with some columns and 'subject' is one of the columns.
I need to get the first 10 letters from the 'subject' field no matter the 'subject' field contains a string with 100 letters.
For example,
Table - tbl.
Columns - id, subject, value.
SQL Query:
SELECT subject FROM tbl WHERE id ='$id';
The result I am getting is, for example
Hello, this is my subject and how are you
I only require the first 10 characters
Hello, thi
I can understand that I can remove the rest of the characters using php substr() but that's not possible in my case. I need to get the excess characters removed by MySQL. How can this be done?
Using the below line
SELECT LEFT(subject , 10) FROM tbl
MySQL Doc.
SELECT SUBSTRING(subject, 1, 10) FROM tbl
Have a look at either Left or Substring if you need to chop it up even more.
Google and the MySQL docs are a good place to start - you'll usually not get such a warm response if you've not even tried to help yourself before asking a question.

find all the name using mysql query which start with the letter 'a'

I want to find the data from table artists where name is start with letter a, b, c.
i.e.
My o/p should contain
Adam
Alan
Bob
Ben
Chris
Cameron
But not GlAin, doC, dolBie
In MySQL use the '^' to identify you want to check the first char of the string then define the array [] of letters you want to check for.
Try This
SELECT * FROM artists WHERE name REGEXP '^[abc]'
You can use like 'A%' expression, but if you want this query to run fast for large tables I'd recommend you to put number of first button into separate field with tiny int type.
Try this:
select * from artists where name like "A%" or name like "B%" or name like "C%"
One can also use RLIKE as below
SELECT * FROM artists WHERE name RLIKE '^[abc]';
Try this simple select:
select *
from artists
where name like "a%"
I would go for substr() functionality in MySql.
Basically, this function takes account of three parameters i.e.
substr(str,pos,len)
http://www.w3resource.com/mysql/string-functions/mysql-substr-function.php
SELECT * FROM artists
WHERE lower(substr(name,1,1)) in ('a','b','c');
try using CHARLIST as shown below:
select distinct name from artists where name RLIKE '^[abc]';
use distinct only if you want distinct values only.
To read about it Click here.