extract database name from database connection string

M Pham 41 Reputation points
2021-01-21T16:24:17.03+00:00

Hi all,

I'm trying to get the database name from connection string. It only works some of the time. Just wonder if I miss anything in my SQL script:

declare @dbconn varchar (500)

set @dbconn = 'Data Source=test-sql01d\NEW_NIDEV01;Initial Catalog=Restricted;Provider=SQLNCLI11.1;Integrated Security=SSPI;Auto Translate=false;'

select substring(@dbconn,(CHARINDEX('g=',@dbconn) + 2),CHARINDEX('T',right(@dbconn,79)))

if my database name is 'tested' then it worked but if my database name is something else like 'Restricted' then it came back with just Restr

SQL Server Integration Services
{count} votes

Answer accepted by question author
  1. Yitzhak Khabinsky 27,011 Reputation points
    2021-01-21T19:06:11.83+00:00

    Please try the following:

    SQL

    DECLARE @dbconn varchar (500)
     , @init_catalog VARCHAR(100)
     , @db_name VARCHAR(100)
     , @separator CHAR(1) = ';'
     , @separator2 CHAR(1) = '=';
    
    SET @dbconn = 'Data Source=test-sql01d\NEW_NIDEV01;Initial Catalog=Restricted;Provider=SQLNCLI11.1;Integrated Security=SSPI;Auto Translate=false;';
    
    SET @init_catalog =  TRY_CAST('<root><r>' + 
                REPLACE(@dbconn, @separator, '</r><r>') + 
                '</r></root>' AS XML).value('(/root/r[contains(./text()[1], "Initial Catalog")])[1]','VARCHAR(100)');
    SELECT @init_catalog AS initial_catalog;
    
    SET @db_name =  TRY_CAST('<root><r>' + 
                REPLACE(@init_catalog, @separator2, '</r><r>') + 
                '</r></root>' AS XML).value('(/root/r/text())[2]','VARCHAR(100)');
    SELECT @db_name AS [db_name];
    

    Output

    +------------+
    |  db_name   |
    +------------+
    | Restricted |
    +------------+
    
    0 comments No comments

3 additional answers

Sort by: Most helpful
  1. Tom Phillips 17,776 Reputation points
    2021-01-21T19:58:56.217+00:00

    The database name is not required in the connection string. So that is not a universal solution to your question.

    1 person found this answer helpful.
    0 comments No comments

  2. M Pham 41 Reputation points
    2021-01-21T19:32:54.777+00:00

    thank you so much YitzhakKhabinsky-0887. It worked!

    0 comments No comments

  3. Alon Zentner 1 Reputation point
    2021-09-14T09:44:09.873+00:00

    CREATE FUNCTION [dbo].[fn_GetDbNameFromConnectionString] (@ConnectionString VARCHAR(MAX) )

    RETURNS sysname
    AS
    BEGIN
    
     DECLARE @Result sysname
     DECLARE @DBNameStart INT
     DECLARE @DBNameEnd INT
     DECLARE @DBNameLength INT
    
     SET @DBNameStart = (SELECT CHARINDEX('Initial Catalog=',@ConnectionString,0)+LEN('Initial Catalog='));
     SET @DBNameEnd = (SELECT CHARINDEX(';',@ConnectionString,@DBNameStart));
     SET @DBNameLength = (SELECT @DBNameEnd-@DBNameStart);
    
     SELECT @Result = SUBSTRING(@ConnectionString,@DBNameStart,@DBNameLength); 
    
    
     RETURN @Result
    
    END
    GO
    
    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.