SQL query for Records containing a Character

  • Thread starter Thread starter WWGD
  • Start date Start date
  • Tags Tags
    Sql
Click For Summary
SUMMARY

The discussion focuses on constructing SQL queries to find records containing a specific character, 'c', in a designated field. The correct SQL syntax for different scenarios is outlined: to find 'c' at the beginning, the query is `SELECT ... FROM table WHERE field LIKE 'c%'`; for 'c' at the end, it is `SELECT ... FROM table WHERE field LIKE '%c'`; and for 'c' in the middle, the appropriate query is `SELECT ... FROM table WHERE field LIKE '%c%'`. The use of the wildcard '%' effectively captures all instances of 'c', including cases where it may be surrounded by other characters or be an empty string.

PREREQUISITES
  • Basic understanding of SQL syntax
  • Familiarity with SQL wildcards
  • Knowledge of SQL SELECT statements
  • Experience with SQL string manipulation
NEXT STEPS
  • Learn about SQL string functions in MySQL
  • Explore advanced SQL query optimization techniques
  • Study the use of regular expressions in SQL queries
  • Investigate performance implications of using wildcards in SQL
USEFUL FOR

Database developers, SQL analysts, and anyone involved in querying relational databases will benefit from this discussion, particularly those looking to refine their skills in string searching within SQL.

WWGD
Science Advisor
Homework Helper
Messages
7,802
Reaction score
13,106
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   Reactions: WWGD

Similar threads

Replies
7
Views
2K
  • · Replies 7 ·
Replies
7
Views
5K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 6 ·
Replies
6
Views
2K
  • · 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