Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

T-SQL - How to search for nth character in a string

I have a situation where the 6th and 7th character in a string need to equal '73'. I tried using LIKE '%73%' but the problem is sometimes SQL Server will hit on other 73s that could be in the string. Is there a way to search only the 6th and 7th characters in the string?

Thank you all so much for the help!

like image 673
Talbert1209 Avatar asked Nov 28 '22 06:11

Talbert1209


2 Answers

Using like, you can do:

where col like '_____73%'

Of course, the other solutions suggesting substring() are also very sensible.

like image 20
Gordon Linoff Avatar answered Dec 19 '22 10:12

Gordon Linoff


Use SUBSTRING ( expression ,start , length )

SELECT mycolumn 
FROM mytable
WHERE SUBSTRING ( mycolumn ,6 , 2 ) = '73'
like image 93
Marek Vitek Avatar answered Dec 19 '22 11:12

Marek Vitek