SQL query for Records containing a Character

  • Thread starter Thread starter WWGD
  • Start date Start date
  • Tags Tags
    Sql
Click For Summary
To query records containing a specific character, such as 'c', in a field using SQL, the following patterns are used: for 'c' at the beginning, the query is "SELECT ... FROM table WHERE field LIKE 'c%'", where '%' acts as a wildcard. For 'c' at the end, the query is "SELECT ... FROM table WHERE field LIKE '%c'". If 'c' is located anywhere in the middle of the field, the correct query is "SELECT ... FROM table WHERE field LIKE '%c%'". This last pattern effectively captures all instances of 'c', including those at the beginning, middle, or end of the string, as '%' can represent an empty string.
WWGD
Science Advisor
Homework Helper
Messages
7,771
Reaction score
12,990
Hi All,
I am trying to do an SQL query for records containing a certain character, say, c , in 'field'

I think if the character c is at the beginning, we use :

Select ... from table
where field like 'c % ' , where % is a wildcard.

Similar for cases where the character is at the end, i.e., we would use :

Select ... from table
where field like '% c '.

But if the character c was somewhere in the middle , would we use :

Select ... from table
where field like '% c % ' ?

EDIT: I assume the last choice, '% c % ' covers all cases, since % may stand for an empty string.
Is this correct?
Thanks.
 
Last edited:
Technology news on Phys.org
Yes, everything you have said is correct.
 
  • Like
Likes WWGD
Learn If you want to write code for Python Machine learning, AI Statistics/data analysis Scientific research Web application servers Some microcontrollers JavaScript/Node JS/TypeScript Web sites Web application servers C# Games (Unity) Consumer applications (Windows) Business applications C++ Games (Unreal Engine) Operating systems, device drivers Microcontrollers/embedded systems Consumer applications (Linux) Some more tips: Do not learn C++ (or any other dialect of C) as a...

Similar threads

Replies
7
Views
2K
  • · Replies 7 ·
Replies
7
Views
4K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 6 ·
Replies
6
Views
1K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 18 ·
Replies
18
Views
4K
  • · Replies 20 ·
Replies
20
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
Replies
5
Views
2K