Q&A for professional and enthusiast programmers
SELECT Site = LEFT(LEFT( RIGHT (name, LEN(name) - CHARINDEX('.',name) ), CHARINDEX('.',RIGHT (name, LEN(name) - CHARINDEX('.',name) )) ), LEN(LEFT( RIGHT (name, LEN(name) - CHARINDEX('.',name) ), CHARINDEX('.',RIGHT (name, LEN(name) - CHARINDEX('.',name) )) ))-1) FROM sys.databases WHERE Name like '%StackExchange.%' and LEFT( RIGHT (name, LEN(name) - CHARINDEX('.',name) ), CHARINDEX('.',RIGHT (name, LEN(name) - CHARINDEX('.',name) )) ) NOT like ''