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
Related
i would like to perform Mysql search & replace with random characters, taken from a list. I cannot use regex, since my version is way prior to 8.
instead of the below,
i would like to change for instance the letter u with one out of (a,e,i,f,k) randomly.
UPDATE products
SET
productDescription = REPLACE(productDescription,
'abuot',
'about');
Is there a mysql command for this task?
Actually my goal is to get in the lastnames column, new names that are not exactly like the real ones, so one could work on "anonymous" data.
I would like to replace all rows in a certain column. Say in table products, in column description, we have data like:
abcud
ieruie
kjuklkllu
uiervfd
With the replace function, we would not want to create something like: replace e with i,
but replace e with one of (a,e,i,f,k)
example desired output:
abced
ierfie
kjiklkllk
aiervfd
like i said, we plan to use this into last names, we plan to replace many characters with random ones from a list, in an effort to create anonymous data in the column that contains last names.
On a next step, i would like to do the same, in order to make anonymous telephone numbers.
example
726456273
827364878
347823472
replace 3 with one of 0-9,
output:
726456279
827664878
547821472
SELECT REPLACE('product abuot Description',
SUBSTRING('product abuot Description', CHARINDEX('abuot', 'product abuot Description') ,5) , 'about')
CREATE FUNCTION smart_replace ( argument TEXT,
search_for CHAR(1),
replace_with TEXT )
RETURNS TEXT
NO SQL
BEGIN
SET argument = REPLACE(argument, search_for, CHAR(0));
REPEAT
SET argument = CONCAT( SUBSTRING_INDEX(argument, CHAR(0), 1),
SUBSTRING(replace_with FROM CEIL(RAND() * LENGTH(replace_with)) FOR 1),
SUBSTRING(argument FROM 2 + LENGTH(SUBSTRING_INDEX(argument, CHAR(0), 1))));
UNTIL NOT LOCATE(CHAR(0), argument) END REPEAT;
RETURN argument;
END
replace e with one of (a,e,i,f,k)
SELECT smart_replace(table.column, 'e', 'aeifk')
replace 3 with one of 0-9
SELECT smart_replace(table.phone, 'e', '0123456789')
I have a use case as below:
I have thousands of records in the database and let's say I am having one column named myValue.
Now the myValue's actual value can be an alphanumeric string where the first two characters are alphabets, the next 6 characters are numbers and the last character is a fixed alphabet let say 'x', which may be or may not be present in the value. (For Example 'AB123456','AB123456x')
So I know the format of the value for myValue field but not know all the actual values as there are lots of records.
Now I want to retrieve all such values for which the value without last character x (For Example, 'AB123456') and the same value with last character x (For Example, 'AB123456x') exists.
So is there any way I can retrieve such data?
I am right now doing trial and error on existing data but have not found success and there are thousands of such rows, so any help on this would be appreciated a lot.
You can do so like this:
SELECT myvalue
FROM t
WHERE myvalue LIKE '________'
AND EXISTS (
SELECT 1
FROM t AS x
WHERE x.myvalue = CONCAT(t.myvalue, 'x')
)
A (most likely) faster alternate is:
SELECT TRIM(TRAILING 'x' FROM myvalue) AS myvalue2
FROM t
GROUP BY myvalue2
HAVING COUNT(DISTINCT myvalue) > 1
I have this column in a table which is comma delimited to separate the values.
Here's the sample data:
2003,2004
2003,2005
2003,2006
2003,2004,2005
2003,2007
I want to get all data that contains only 1 comma.
I've been playing around with the '%' and '_' wildcards, but I can't seem to get the results I need.
SELECT column FROM table WHERE column like '%_,%'
Replace the , with '' empty set then take the original length less the replaced length. if 1 then only 1 comma if > 1 then more than 1 comma.
The length difference would represent the number of commas.
Length(column) - length(Replace(column,',','')) as NumOfCommas
or
where Length(column) - length(Replace(column,',','')) =1
While this may solve the problem, I agree with what others have indicated. Storing multiple values in a single column in a RDBMS is asking for more trouble. Better to normalize the data and get it to at least 3rd Normal form!
You can also use find_in_set() method which searches a value in comma separated list, by picking the last value of column using substring_index we can then check result of find_in_set should be 2 so that its the second and last value from list
select *
from demo
where find_in_set(substring_index(data,',',-1),data) = 2
Demo
Maybe another solution is to use regular expression in your case it can look like this ^[0-9]{4},[0-9]{4}$ :
SELECT * FROM MyTable WHERE ColName REGEXP '^[0-9]{4},[0-9]{4}$'
Or if you want all non comma one or more time :
SELECT * FROM MyTable WHERE ColName REGEXP '^[^,]*,[^,]*$'
I'm looking for a way to get a row from a tabla which have a column data type of string. This column could have values as follows:
1. "1,2,3,4,5"
2. "X,3,4,5,8"
3. "X,X,3,4,5"
4. "1,2,3,4,X"
5. "1,3,4,X,X"
and so on, ...
I want to accomplish a search for a String like
"1,2,3,4,5"
I tried with a
SELECT *
FROM *table_name*
WHERE *column* LIKE '%1,2,3,4,5%';
hoping this query could retrieve at least three results (in the example, first, third and forth strings) but it returns only the first string, because of course it's the only string that matches with the specified criteria. Anyone knows a way for me to accomplish this achievement?
I assume the X listed is literally the X character - if so, try
SELECT * FROM table WHERE '1,2,3,4,5' REGEXP REPLACE(column, 'X', '.')
I need to find in a row of my table all the values that are repetitive, e.g. 123text123text (there might be a line break between the repetitive sub-strings.
So I need an SQL query that tells PhpMyAdmin to return all instances were sub-string first half of value == sub-string second half of value.
--UPDATE I don't think i need regex for my query it just needs to select values from the row were sub-string from begin until half of string equals sub-string from half of string until end
Thanks!
ok it was as simple as
SELECT * FROM table_name WHERE substring(column, (length(column)/2)+1) like substring(column, 1, (length(column)/2)-1)