w3resource

AdventureWorks Database: Find the position of the string 'yellow'

SQL Query - AdventureWorks: Exercise-59 with Solution

59. From the following table write a SQL query to locate the position of the string "yellow" where it appears in the product name.

Sample table: production.product


Click to view Full table

Sample Solution:

SELECT name, strpos(name,'Yellow') as "String Position" 
from production.product
where strpos(name,'Yellow')>0;

Sample Output:

name                         |String Position|
-----------------------------+---------------+
Paint - Yellow               |              9|
Road-550-W Yellow, 38        |             12|
Road-550-W Yellow, 40        |             12|
Road-550-W Yellow, 42        |             12|
Road-550-W Yellow, 44        |             12|
Road-550-W Yellow, 48        |             12|
ML Road Frame-W - Yellow, 38 |             19|
ML Road Frame-W - Yellow, 40 |             19|
ML Road Frame-W - Yellow, 42 |             19|
ML Road Frame-W - Yellow, 44 |             19|
ML Road Frame-W - Yellow, 48 |             19|
...

SQL AdventureWorks Editor:

Practice Online


Contribute your code and comments through Disqus.

Previous: Employee name, email separated by a new line.
Next: Join the name, color etc.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Share this Tutorial / Exercise on : Facebook and Twitter

SQL: Tips of the Day

T-SQL Cast versus Convert?

CONVERT is SQL Server specific, CAST is ANSI.

CONVERT is more flexible in that you can format dates etc. Other than that, they are pretty much the same. If you don't care about the extended features, use CAST.

Ref : https://bit.ly/3VKT5M8