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
Related
I have a table that contains multiple fields - let's say FieldA, FieldB etc. and finally Location. The Location field has values such as:
http://192.168.1.10/location?n=5
http://192.168.1.10/location?n=8
http://192.168.15.6/location?n=1
http://192.168.0.9/location?n=11
http://192.168.15.6/location?n=5
http://192.168.0.9/location?n=6
http://192.168.1.10/location?n=2
I need to get the unique values of the IP addresses in the Location field. In other words, from the above example data, I should get
http://192.168.1.10
http://192.168.15.6
http://192.168.0.9
Based on this answer, I am using the following SQL - without much luck
SELECT * FROM `table` WHERE FieldA = 'Example' GROUP BY (SELECT SUBSTRING_INDEX("`table`.Location", "/", 3))
The above gives me just a single record. What am I doing wrong?
Making judicious use of SUBSTRING_INDEX:
SELECT DISTINCT SUBSTRING_INDEX(SUBSTRING_INDEX(Location, '/', 3), '/', -1) AS distinct_ips
FROM yourTable;
Demo
For an explanation on how the above logic works, consider the location value http://192.168.1.10/location?n=5. The inner call to SUBSTRING_INDEX returns http://192.168.1.10, which is everything to the left of the third forward slash. Then, the outer call returns everything to the right of the last forward slash, which leaves us with the IP address.
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.
I'm trying to find if any word of a given string is contained within a column in a mySQL table.
Example
String: 'Company LLC'
Column: 'LLC'
I've tried the below query but no dice.
select * from table
where column sounds like '%Company LLC%'
Try this:
select * from table
where column rlike replace('Company LLC', ' ', '|')
This does a regex match. In regex, the pipe char | means "or". The resulting regex means "Company or LLC". In MySQL, rlike matches when any part of the value matches (rather than the whole column matches), so you don't need ".*" on each end.
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;
I'm trying to select distinct substring values of a field and count the number of instances of a char in that selection.
I've found this wonderful post which answers half of it.
So, so far, i can count the instances of a char in my field, it works great. Now the even harder part, what if i select a piece of string using :
SELECT DISTINCT SUBSTRING_INDEX(my_field, '-', -1) AS chunk
In this case i'm only selecting the last part of the string (everything after the last'-'). How can i apply this formula to chunk (trying to count the number of instances of '_' in the new string ? :
(LENGTH(chunk) - LENGTH(REPLACE(chunk, '_', ''))) / LENGTH('_')
I know i shoud be using HAVING to make operation on chunk as it's not a real field, but how can i do something like :
SELECT DISTINCT SUBSTRING_INDEX(my_field, '-', -1) AS chunk, (LENGTH(chunk) - LENGTH(REPLACE(chunk, '_', ''))) / LENGTH('_') AS total FROM my_field HAVING total < 2
The problem here is that i can't use 'chunk' in the last part since it's not a field..
The problem here is that i can't use 'chunk' in the last part since it's not a field..
Replace 'chunk' in the last part with
SUBSTRING_INDEX(my_field, '-', -1)
Don't know what's the problem?