Tags

, , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , ,


SQL Puzzle | Find Strings that contains % Character

In this puzzle you have to find strings from a table where string itself contains % character.

Please check out the sample input values and sample expected output below.

Sample Input

Strs
Pawan
Pawan%
Pawan%Kumar
%

Expected Output

Strs
Pawan%
Pawan%Kumar
%

Script

Use below script to create table and insert sample data into it.

--



CREATE TABLE TestPercent
(
	Strs VARCHAR(100)
)
GO

INSERT INTO TestPercent
SELECT 'Pawan'
UNION ALL
SELECT 'Pawan%'	
UNION ALL
SELECT 'Pawan%Kumar'
UNION ALL
SELECT '%'
GO



--

Rules/Restrictions

The solution should be should use “SELECT” statement or “CTE”.
Add your solution(s) in the comments section or send you solution(s) to pawankkmr@gmail.com

SOLUTION – 1 | Using ESCAPE


--

SELECT  Strs 
		FROM TestPercent
WHERE 
		Strs LIKE '%\%%' ESCAPE '\'
	
--

SOLUTION – 2 | Using Square Brackets


--

SELECT 
		Strs 
		FROM TestPercent
WHERE 
		Strs LIKE '%[%]%'	
	
--

Add a comment if you have any other solution in mind. I would love to learn it. We all need to learn.

Enjoy !!! Keep Learning

Pawan Khowal Http://MSBISkills.com

Advertisements