Yaser Ahmed
Yaser Ahmed

Reputation: 559

How to use the LIKE keyword in SQL?

I want to write an SQL query using the LIKE keyword. It should search the first character or the starting character of my column with the search parameter.

Is there a specific syntax for doing this?

Upvotes: 2

Views: 2786

Answers (4)

Tomalak
Tomalak

Reputation: 338406

For a parameterized query, which you seem to have ("search the first character [...] with the search parameter"), use this:

SELECT *
FROM yourtable
WHERE yourcolumn LIKE ? + '%'

Upvotes: 1

Fenton
Fenton

Reputation: 251262

Here's two examples...

SELECT FirstName
FROM tblCustomer
WHERE FirstName LIKE 'B%'

The % sign is the wildcard, so this would return all results where the First Name starts with B.

This might be more efficient though...

SELECT FirstName
FROM tblCustomer
WHERE LEFT(FirstName, 1) = 'B'

Upvotes: 1

Ezekiel Rage
Ezekiel Rage

Reputation: 581

Try

select * from table where column like 'c%'

where 'c' is the character you want

Upvotes: 3

Lasse V. Karlsen
Lasse V. Karlsen

Reputation: 391724

Is this what you're looking for

SELECT *
FROM yourtable
WHERE yourcolumn LIKE 'X%'

This will find all rows where yourcolumn starts with the letter X.

To find all that ends with X:

...
WHERE yourcolumn LIKE '%X'

...and contains an X...

...
WHERE yourcolumn LIKE '%X%'

Upvotes: 9

Related Questions