Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mysql: Find rows where column value ends with a specific substring

Tags:

I have a column url where all the values are urls. I'm trying to update the value of a different column where-ever the aforementioned url ends with .pdf.

Can anyone help me with this? I haven't found an answer here on SO.

like image 247
Phil Avatar asked Mar 11 '13 18:03

Phil


People also ask

How do you check if a string ends with a substring in SQL?

In Standard SQL, we use the ends_with() function to determine if a substring is a suffix of another. The function will take the value1 and value2 as string or a sequence of bytes. It will then evaluate if value2 is a suffix of value1.

How do you check if a string contains a substring in MySQL?

MySQL query string contains using LOCATE We will be using the LOCATE() function for the solution. LOCATE(substr, str) function returns the first occurrence of the substring passed in as parameters. Here substr is the substring passed in as the first argument, and str is the string passed in as the second argument.

How do I select the last 5 records of a table?

METHOD 1 : Using LIMIT clause in descending orderof specified rows from specifies row. We will retrieve last 5 rows in descending order using LIMIT and ORDER BY clauses and finally make the resultant rows ascending. Since Employee table has IDs, we will perform ORDER BY ID in our query.


2 Answers

I might be oversimplifying... but based on your description, would LIKE solve your problem?

UPDATE YourTable SET DifferentColumn = 'NewValue' WHERE Url LIKE '%.pdf' 
like image 199
Michael Fredrickson Avatar answered Oct 22 '22 15:10

Michael Fredrickson


You can use a LIKE condition with a leading wildcard. %.pdf means the column must end in .pdf

UPDATE mytable SET newcolumn = 'PDF found' WHERE yourcolumn LIKE '%.pdf' 

Alternatively you could use the right() function

UPDATE mytable SET newcolumn = 'PDF found' WHERE right(yourcolumn,4) = '.pdf' 
like image 29
Matt Busche Avatar answered Oct 22 '22 16:10

Matt Busche