Sql query to select all names that start with a given letter without like operator04:33

  • 0
Published on May 10, 2017

Text Article

Slides

SQL Server Interview Questions and Answers text articles & slides

SQL Server Interview Questions and Answers playlist

All Dot Net and SQL Server Tutorials in English

All Dot Net and SQL Server Tutorials in Arabic

In this video we will discuss writing a SQL query to retrieve all student names that start with letter ‘M’ without using the LIKE operator.

If the interviewer has not mentioned not to use LIKE operator, we would have written the query using the LIKE operator as shown below.
SELECT * FROM Students WHERE Name LIKE ‘M%’

We can use any one of the following 3 SQL Server functions, to achieve exactly the same thing.
CHARINDEX
LEFT
SUBSTRING

The following 3 queries retrieve all student rows whose Name starts with letter ‘M’. Notice none of the queries are using the LIKE operator.
SELECT * FROM Students WHERE CHARINDEX(‘M’,Name) = 1
SELECT * FROM Students WHERE LEFT(Name, 1) = ‘M’
SELECT * FROM Students WHERE SUBSTRING(Name, 1, 1) = ‘M’

Enjoyed this video?
"No Thanks. Please Close This Box!"