Technical Article

Extract default & Named Instance from @@servername

,

Extract default & Named Instance from @@servername.

Print all characters before and after \ to find default and named instance name.

Copy and Paste the script

Run it

Declare @servername sysname, @len INT, @i INT, @Left varchar(max), @right varchar(max)
SET @SERVERNAME = (SELECT @@servername);
--SELECT @SERVERNAME AS [SERVER NAME];

IF @SERVERNAME like '%\%'
BEGIN

SET @Left= (select Left(@SERVERNAME, charindex('\', @SERVERNAME) - 1))
SELECT @Left AS [DEFAULT SERVER NAME]

SET @len = (SELECT LEN(@SERVERNAME))

--SELECT @LEN
SET @right = (SELECT SUBSTRING(@SERVERNAME,Charindex ('\', @SERVERNAME)+1,@len))

SELECT @right AS [SQL INSTANCE NAME]

END
ELSE
BEGIN
    SELECT @left  = @SERVERNAME
SELECT @Left AS [DEFAULT SERVER NAME]
SELECT @right = 'DEFAULT'
SELECT @right AS [SQL INSTANCE NAME]
END

Rate

3.67 (3)

You rated this post out of 5. Change rating

Share

Share

Rate

3.67 (3)

You rated this post out of 5. Change rating