I need to remove the last part of a string in a column where I have a field named "path" that looks like:
images/prop/images/2034/22399_2034.JPG
I need everything after the last "/" to be deleted, in order to have
images/prop/images/2034/
instead of
images/prop/images/2034/22399_2034.JPG
I have no idea if this is possible. Thanks.
You can combine MySQL's TRIM() and SUBSTRING_INDEX() functions:
SELECT TRIM(TRAILING SUBSTRING_INDEX(path, '/', -1) FROM path)
FROM my_table
See it on sqlfiddle.
Related
Because there are '-' in front of every string in every row, I wanna remove them and make every string data upper at the same time.
I tried the script below, but it didn't work. The ‘-‘ still existed.
My script:
SELECT DISTINCT TRIM(LEADING '-' FROM UPPER(breed)) AS 'upper_breed'
FROM dogs
ORDER BY breed;
Thanks
Your issue is within the ORDER BY clause: there's no "breed" column in your SELECT clause. You should rather use "upper_breed":
SELECT DISTINCT TRIM(LEADING '-' FROM UPPER(breed)) AS 'upper_breed'
FROM dogs
ORDER BY 'upper_breed';
Check the demo here.
Note: avoid using quotes for column names, or if you want to, use backticks (like upper_breed).
I have a column in database and having value like this
course_repeatfkfjkjfjkfer_10_topics_0_presentation_link
course_repeatfkfjfkfkfklfflkflkfs_1_presentation_link
course_repeatfkfjfkfkfklfflkflkfs_2_presentation_link
coursek_epeatfkfjfkfkfklfflkflkfs_10_presentation_link
course_hdhdhhdhdjhdrepeatfkfjfkfkfklfflkflkfs_21_presentation_link
and so on.
I need to pick 0,1,2,10,21, number before _presentation_link , But i need this in mysql as well
i used substr in mysql, but that is not working. Any idea?
Thanks
One option would be to use a combination of SUBSTRING_INDEX() and REPLACE():
SELECT SUBSTRING_INDEX(REPLACE(col, '_presentation_link', ''), '_', -1)
FROM yourTable
Taking course_repeatfkfjkjfjkfer_10_topics_0_presentation_link as an example, after the replacement, this would become:
course_repeatfkfjkjfjkfer_10_topics_0
The call to SUBSTRING_INDEX() then grabs everything appearing after the final underscore, which is the number you want to capture.
Demo here:
SQLFiddle
You can use substring_index twice like this:
select substring_index(substring_index(col, '_', -3), '_', 1)
from t
Demo
I am trying to remove everything before the third / in a column. For example: If there is a URL in the coloumn such as
http://www.example.com/example1/example2?=testest123
I would like to remove everything (not including the thrid slash) so i will be left with something like this
/example1/example2?=testest123
I have tried using this but it only removes everything from first "/" and i can't work out how to get it to count to the third then remove.
update table
set column2 = substring(column1, instr(column1, '/') + 1);
Thanks.
To start, you can use the SUBSTRING_INDEX function to get the characters of the string leading up to the third (or fourth slash, in your example) like this:
SELECT SUBSTRING_INDEX(val, '/', 4)
FROM myTable;
You can use the REPLACE() function to remove that substring by replacing it with an empty string, like this:
SELECT REPLACE(val, SUBSTRING_INDEX(val, '/', 4), '')
FROM myTable;
Now, to update your table, simple rewrite the query to set the value to the one above:
UPDATE myTable
SET val = REPLACE(val, SUBSTRING_INDEX(val, '/', 4), '');
NOTE that if there are less than four occurrences of a forward slash, SUBSTRING_INDEX will return the entire string, and therefore completely replacing the entire string by an empty value so you should be very careful when preforming this update.
Here is an SQL Fiddle example with your sample text, and one that I wrote with fewer slashes to demonstrate the last point.
That's a bit messy, but you can try this :
UPDATE table
SET column2 = SUBSTRING(column1,
LOCATE('/', column1,
LOCATE('/', column1,
LOCATE('/', column1))+1)+1)
I have values stored like this in a field 1,255,230,265.
Is there a function in MySQL that will give me the second value in that string? In this case it'll be 255.
I tried using locate, but that does not seem to be meant for this.
Try this
select SUBSTRING_INDEX(SUBSTRING_INDEX(field_name,',',2),",",-1) from table_name
You might want to use SUBSTRING_INDEX() function.
SELECT SUBSTRING_INDEX(SUBSTRING_INDEX(field,',',2),',',-1)
FROM yourTable.
This grabs everything infront of the second comma, then grabs everything after the last comma (-1)
Try This
select * from Table1 where ',' + Nos+ ',' like '%,255,%'
Refer:
MySQL query finding values in a comma separated string
mysql check if numbers are in a comma separated list
Use FIND_IN_SET() function:
SELECT *
FROM tableA
WHERE FIND_IN_SET(255, columnName)
OR
Use LIKE operator
SELECT *
FROM tableA
WHERE CONCAT(',', columnName, ',') LIKE '%,255,%'
i have a mysql table with this sort of data
TACOMA, Washington, 98477
Now i have thousands of such rows. I want the data to be manipulated in such a manner that it appears like:
TACOMA, Washington
Is it possible though mysql or do i have to manually do it.
You can use :
SELECT SUBSTRING_INDEX('TACOMA, Washington, 98477', ',', 2)
You can read more here.
And the update statement :
UPDATE my_table
SET my_col = SUBSTRING_INDEX(my_col, ',', 2)
Where you need to replace my_table with your table name and my_col with the column you need to be updated.
Possibly this way. Count the number of commas (by checking the length against the length with all the commas removed) and then use SUBSTRING_INDEX to get the string up to the number of commas:-
SELECT SUBSTRING_INDEX(col, ',', LENGTH(col) - LENGTH(REPLACE(col, ',', '')))
FROM SomeTable
substring_index(col, ',',-1)
will give the string from last index of comma to end of string
replace(col,concat(',',substring_index(col, ',',-1)),'')