Tuesday, November 11, 2008

Get a List of Unique Servers from a list of SQL Instances

Here's an example of pulling just the server name from a list of SQL Server instances.

CREATE TABLE [dbo].[Instances](
[QID] [smallint] IDENTITY(1,1) NOT NULL,
[Instance] [varchar](128) NOT NULL
--additional fields and table options not shown
)
GO
--load the table with data
SELECT DISTINCT --eliminate duplicates
CASE
WHEN
charindex('\', [instance]) > 0 THEN --if the instance name includes a slash
substring([instance], 1, charindex('\', [instance]) - 1) --take only the part before the slash
ELSE
[instance] --otherwise grab the whole name
END [ServerName]
FROM
[dbo].[instances]

No comments: