Wednesday, December 18, 2024

SQL LIKE Keyword

 The SQL LIKE keyword is used in a WHERE clause to search for a specified pattern in a column. It is often used with wildcard characters:

  1. % (percent sign): Represents zero or more characters.
  2. _ (underscore): Represents a single character.

Syntax:

SELECT column_name
FROM table_name
WHERE column_name LIKE pattern;

Example with % (Zero or more characters):

SELECT * 
FROM Employees
WHERE Name LIKE 'A%';

This query will return all records where the Name starts with "A".

Example with _ (Single character):

SELECT * 
FROM Employees
WHERE Name LIKE '_r%';

This query will return all records where the Name has "r" as the second character.

Additional Examples:

  • LIKE '%Smith': Finds any values that end with "Smith".
  • LIKE 'J_n': Finds any values where the second letter is "n", like "Jan" or "Jon".

The LIKE keyword is case-insensitive in some databases (like MySQL) and case-sensitive in others (like PostgreSQL), unless you use a specific collation or function to change this behavior.

No comments:

Post a Comment