Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL LIKE statement to find a substring at any location except the start of the string

I have a SQL query SELECT * FROM table WHERE column LIKE '%pdd%'.

The problem is I need to get all results excluding those which start with "pdd", by which I mean find everything where "pdd" is not at the beginning. How could it be done?

I do need to match "pdd" when it is not at the beginning of the column.

like image 866
Anthony Avatar asked Dec 16 '09 22:12

Anthony


People also ask

How do I select a part of a string in MySQL?

SUBSTRING() function in MySQL function in MySQL is used to derive substring from any given string . It extracts a string with a specified length, starting from a given location in an input string. The purpose of substring is to return a specific portion of the string.

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.

Which MySQL function will return the position of the first occurence of a substring in a string pick one option?

INSTR() function MySQL INSTR() takes a string and a substring of it as arguments, and returns an integer which indicates the position of the first occurrence of the substring within the string.

Which SQL function is used to return the location of a substring in a string?

CHARINDEX function in SQL queries The CHARINDEX() function returns the substring position inside the specified string. It works reverse to the SUBSTRING function. The substring() returns the string from the starting position however the CHARINDEX returns the substring position.


1 Answers

I assume you mean all rows that match "pdd" except those where "pdd" is at the beginning.

SELECT * FROM table WHERE column LIKE '_%pdd%'.

The "_" wildcard in LIKE predicates means "one of any character," equivalent to "." in regular expressions.

like image 120
Bill Karwin Avatar answered Sep 23 '22 02:09

Bill Karwin