SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


How to parse connection strings on a column in SQL Server 2008


How to parse connection strings on a column in SQL Server 2008

Author
Message
Kuzey
Kuzey
Valued Member
Valued Member (57 reputation)Valued Member (57 reputation)Valued Member (57 reputation)Valued Member (57 reputation)Valued Member (57 reputation)Valued Member (57 reputation)Valued Member (57 reputation)Valued Member (57 reputation)

Group: General Forum Members
Points: 57 Visits: 519
I want to extract database names from a varchar column storing connection strings.

A sample value is; 'server=SVR1;database=DB1;uid=user1;pwd=pass1;enlist=true;'

Each record might have a slightly different connection string format; Uid before Database, or no Enlist etc. 'server=SVR1;uid=user1;database=DB1;pwd=pass1;'

I have a few parsing scripts but they are not useful for this specific extraction.

Basically, I want the string starts after 'database=' and ends at the following ';'.

Thanks,

Kuzey
Kuzey
Kuzey
Valued Member
Valued Member (57 reputation)Valued Member (57 reputation)Valued Member (57 reputation)Valued Member (57 reputation)Valued Member (57 reputation)Valued Member (57 reputation)Valued Member (57 reputation)Valued Member (57 reputation)

Group: General Forum Members
Points: 57 Visits: 519
Found it.

SELECT
substring(substring(dbconnection,CHARINDEX('Database=',dbconnection)+9, LEN(dbconnection)),1,CHARINDEX(';',substring(dbconnection,CHARINDEX('Database=',dbconnection)+9, LEN(dbconnection)))-1)
FROM Connections


Thanks,

Kuzey
dwain.c
dwain.c
SSCertifiable
SSCertifiable (7.3K reputation)SSCertifiable (7.3K reputation)SSCertifiable (7.3K reputation)SSCertifiable (7.3K reputation)SSCertifiable (7.3K reputation)SSCertifiable (7.3K reputation)SSCertifiable (7.3K reputation)SSCertifiable (7.3K reputation)

Group: General Forum Members
Points: 7319 Visits: 6431
You can expose pretty much any parameter you want regardless of position using a pattern-based string splitter like this:


WITH SampleData (ID, ConnectionString) AS
(
SELECT 1, 'server=SVR1;database=DB1;uid=user1;pwd=pass1;enlist=true'
UNION ALL SELECT 2, 'database=DB1;server=SVR1;uid=user1;pwd=pass1;enlist=true'
UNION ALL SELECT 3, 'server=SVR1;uid=user1;pwd=pass1;database=DB1;enlist=true'
),
Params AS
(
SELECT *
FROM SampleData
CROSS APPLY PatternSplitCM(ConnectionString, '[;=]')
WHERE [Matched]=0
)
SELECT ID, dbname=Item
FROM Params a
WHERE ItemNumber =
(
SELECT 2+ItemNumber
FROM Params b
WHERE a.ID = b.ID AND b.Item = 'database'
);




PatternSplitCM is a utility function that can be found in the 4th article in my signature links.


My mantra: No loops! No CURSORs! No RBAR! Hoo-uh!

My thought question: Have you ever been told that your query runs too fast?

My advice:
INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.


Need to UNPIVOT? Why not CROSS APPLY VALUES instead?
Since random numbers are too important to be left to chance, let's generate some!
Learn to understand recursive CTEs by example.
Splitting strings based on patterns can be fast!
My temporal SQL musings: Calendar Tables, an Easter SQL, Time Slots and Self-maintaining, Contiguous Effective Dates in Temporal Tables
Kuzey
Kuzey
Valued Member
Valued Member (57 reputation)Valued Member (57 reputation)Valued Member (57 reputation)Valued Member (57 reputation)Valued Member (57 reputation)Valued Member (57 reputation)Valued Member (57 reputation)Valued Member (57 reputation)

Group: General Forum Members
Points: 57 Visits: 519
Great, Thanks.
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search