extracting previous word from string in SQL - mysql

I have an SQL table with employee biographies.
One of the columns is named Biography.
Some entries describe how many years the employee has worked at the company.
An example:
"Gary has worked for us for 8 years"
I want to extract the number of years each employee has worked for the company.
I need an sql query that will identify for each entry the word "years", extract the previous word as an INT, and store this number in the same row as a new column.
Can somebody help explain how to do this for me?
Thanks.

MySQL SUBSTRING_INDEX() returns the substring from the given string before a specified number of occurrences of a delimiter.
Syntax:
SUBSTRING_INDEX(string, delimeter, count)
string: column_name or a constant string
delimeter: string before which we are finding the prefix
count: The number of times to search for the delimiter
Taking tablename as test and column name which we need to update as new_column and column containing string as query_string
UPDATE test SET new_column=(SELECT SUBSTRING_INDEX(TRIM(SUBSTRING_INDEX(query_string, 'years', 1)), ' ', -1 ) )
Hope this helps!!

In MySQL, you can use substring_index():
select substring_index(trim(substring_index(col, 'years', 1)), ' ', -1)
Here is a db<>fiddle.

Related

MYSQL: Get substring from column value?

I have the following Varchar data in a column in my MYSQL table:
Blank_Person_ID_776
Person_999
I want to extract the final number after the underscore to a variable (in this case 776) in order to use it in a query. I.e. ignore any underscore but the last one.
How can I do so?
I would like my final query to be as follows:
SET #personId= //query to get id;
Update Person set tracking_id = #personId where tracking_id is null;
If you want the final value after the last '_', use substring_index():
select substring_index(<whatever>, '_', -1)
If you specifically want the final number in the string, even when there are characters after:
select regexp_replace(<whatever>, '.*_([0-9]+)[^0-9]*$', '$1')

Searching a string in a column

can anyone help me. i have a DB in mysql, and need to search for a string in a particular column.
the field is var char, and contains various serial number, divided by the character "/".
example
613003593/8876572/TJMC49
the problem is searching in the string. If i use like, it will work most of the times, but not always, because if i do a like '%13003593%' it will return one row, when that is not true, the saved value is 613003593. how can i search, the string.
on the example there are 3 strings divided, and i need to search all of them.
apologies for my english
For the first part of the serial number,
SELECT * FROM table_name
WHERE SUBSTRING_INDEX(serial_number, '/', 1) = '613003593';
For the 2nd part,
SELECT * FROM table_name
WHERE SUBSTRING_INDEX(SUBSTRING_INDEX(serial_number, '/', 2), '/', -1)='8876572';
For the last part,
SELECT * FROM table_name
WHERE SUBSTRING_INDEX(SUBSTRING_INDEX(serial_number, '/', 3), '/', -1)='TJMC49';
Check How to split and search in comma-separated string in MySQL

In MySQL, how can I extract a string after a specific string pattern?

I'm using MySQL 5.5.37. One of my VARCHAR columns returns string data of the form
Description Number 9 MOre description
Every string will have the word "Number" followed by an actual number, whether it be one or multiple digits.
My quesiton is how do I select only the portion of the string that follows the word "Number," taht is the number? In the example above, my select statement would return "9".
Try using substring_index to get the number if there is numeric value after "Number" else null:
select
case
when col regexp '.*Number [0-9]+.*'
then substring_index(substring_index(col, 'Number ', -1), ' ', 1)
end
from t;
Demo

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;

How to display two columns of different types in one column?

I want to display two columns of different types or two column data of same type that will be displayed in one column.
The types are date + time, or varchar + varchar etc
I know how to concat strings (add a string to one column) but can't seem to do it for two columns data.
Say I want to display two columns both varchar type, fname + lname = Ajay Punja
Or
Lname + DOB = Punja 01/01/2001
I tried using single and double pipes, plus signs etc but always returns 0.
Is it because I need to convert two different data types into one matching data type? But, both varchar types returns 0.
I think it will help you.
SELECT CONCAT(2, ' test') as result form table_name;
It is also possible to convert a number to a string explicitly using the CAST() function. Conversion occurs implicitly with the CONCAT() function because it expects string arguments. e.g.
SELECT 38.8, CAST(38.8 AS CHAR);
My answer thanks to everyone here :)
SELECT CONCAT(fname, ' ', DOB) as Results FROM Person;
SELECT CONCAT(fname, ' ', lname) as Results FROM Person;
Now I understand how to use CONCAT properly. Before I thought for each bracket it must contain only one attribute (or string) and then CONCAT it with enough bracket of data, but clearly that's wrong.
Wrong example (fail attempt):
SELECT CONCAT(fname, '') + (' ', lname)) as Results FROM Person;