All posts by David Hall

About David Hall

David Hall is Director of Software Development at Inc. (, Canada’s original .CA Registrar and one of the country’s leading webhosting and Internet solutions companies. David has had a wide variety of experiences in different industries, originally in State/Provincial Lottery Systems (GTECH/Riptide Technologies), to Open Pit Mine Fleet Management Systems (Wenco International) to Internet Technologies with David is an active member in many .NET and SQL Server groups in the lower mainland as well as for writing articles for the and blog.

T-SQL – Find the last occurrence of a sub-string

Working in the domain name business, I frequently have to find the last occurrence of a sub-string in T-SQL in order to properly parse out the domain extension. For example, if I have the domain name ‘’ and I want to know whether the domain is a .com or .ca or other domain extension, the fastest T-SQL command I have found that works is:

len(''))) AS "First Part",
CHARINDEX('.',REVERSE('')))) AS "Second Part"

The result of this TSQL query, is two values:





This T-SQL works well to find the last occurrence of a sub-string within a large string of text that is separated by a character like a period, comma or other character.