+2 votes
in Databases by (11k points)
I need SQL code to check if a particular string/substring is present in one of the columns of a table.

1 Answer

0 votes
by (15.1k points)

The LOCATE() function of MySQL returns the position of the first occurrence of a substring in a string. Use can use this function to find if your string/substring is present in a column. Here is the SQL query you can try:

SELECT * FROM tableName WHERE LOCATE(yourString, columnName) > 0

If your substring starts from the very first position in the string, you can try the following SQL code:

SELECT columnName, LOCATE(yourString, columnName) as positionFound FROM tableName

...