Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Splitting from a string

Author  Topic 

sql_chaser
Starting Member

33 Posts

Posted - 2014-12-02 : 14:08:39
I'm trying to extract the servername and other details from the string.Is there any function I can use to split them from a given string.

DECLARE @Tmp TABLE (Id INT,Name VARCHAR(500))
INSERT @Tmp SELECT 1,'from SQLEDW.PRE_KS_ODS.dbo.Events et '
INSERT @Tmp SELECT 2,'join SQLEDW.PRE_MS_ODS.dbo.Event_Methods el on el.id = et.Event_ID and et.load_date = el.LOAD_DATE '
INSERT @Tmp SELECT 3,'edw.dbo.MASTER_EVENT a , '
INSERT @Tmp SELECT 4,' (SELECT ''SQLEDW.edw.dbo.MSEDR_DS_TBL_D'' AS TableName, COUNT(*) AS EventNo '

SELECT *
FROM @Tmp

ServerName		DatabaseName		SchemaName		TableName

SQLEDW PRE_KS_ODS dbo Events
SQLEDW PRE_MS_ODS dbo Events
NULL edw dbo MASTER_EVENT
SQLEDW edw dbo MSEDR_DS_TBL_D


gbritton
Master Smack Fu Yak Hacker

2780 Posts

Posted - 2014-12-02 : 14:18:14
check out the PARSENAME function: http://msdn.microsoft.com/en-us/library/ms188006.aspx
Go to Top of Page

sql_chaser
Starting Member

33 Posts

Posted - 2014-12-02 : 14:22:07
The challenge I'm having is to extract the content before using the PARSE function
Go to Top of Page

gbritton
Master Smack Fu Yak Hacker

2780 Posts

Posted - 2014-12-02 : 15:17:03
Use CHARINDEX or PATINDEX functions to lop off the start and end bits. e.g.

substring(name, charindex(' ')+1, len(name))

would chop off the word 'from' in the first row. It gets tedious, I know, but you'll get there eventually.
Go to Top of Page

sql_chaser
Starting Member

33 Posts

Posted - 2014-12-02 : 16:12:34
Thanks gbritton for the response...When I use the charindex should chop the 'from' and 'join'...How do I get the SQLEDW.PRE_MS_ODS.dbo.Event_Methods in the second case..
Go to Top of Page

sql_chaser
Starting Member

33 Posts

Posted - 2014-12-02 : 16:20:53
In the first and 2nd it will chop off "from" and "join" but in the 3rd it chopping the value...
SELECT substring(name,charindex(' ',Name)+1,LEN(name)),Name
FROM @Tmp
Go to Top of Page

gbritton
Master Smack Fu Yak Hacker

2780 Posts

Posted - 2014-12-02 : 20:02:36
Sure, you query needs to be smarter and take into account object refs without verbs. e.g. you could look for a '.' preceding a ' ' as such an indication. All possible with the CHARINDEX function.

Just get creative!
Go to Top of Page

sql_chaser
Starting Member

33 Posts

Posted - 2014-12-02 : 21:18:50
Gbritton....Thanks for the support if possible can you please help with above example combination and then I can use that example for all the other combination as I might be using this against multiple stored procedure queries
Go to Top of Page

gbritton
Master Smack Fu Yak Hacker

2780 Posts

Posted - 2014-12-03 : 09:17:11
This ought to give you something to go on:


DECLARE @Tmp TABLE (Id INT,Name VARCHAR(500))
INSERT @Tmp SELECT 1,'from SQLEDW.PRE_KS_ODS.dbo.Events et '
INSERT @Tmp SELECT 2,'join SQLEDW.PRE_MS_ODS.dbo.Event_Methods el on el.id = et.Event_ID and et.load_date = el.LOAD_DATE '
INSERT @Tmp SELECT 3,'edw.dbo.MASTER_EVENT a , '
INSERT @Tmp SELECT 4,' (SELECT ''SQLEDW.edw.dbo.MSEDR_DS_TBL_D'' AS TableName, COUNT(*) AS EventNo '
INSERT @Tmp SELECT 3,'edw.dbo.MASTER_EVENT'

select name, _1st.blank, _1st.dot, name.start, just.thename, _4part.*
from @tmp
cross apply (select charindex(' ', name), charindex('.', name)) _1st(blank, dot)
cross apply (
select substring(name,
case
when _1st.blank = 0
then 1
when _1st.dot = 0
then _1st.blank + 1
when _1st.dot > _1st.blank
then _1st.blank + 1
else 1
end,
len(name) )
) name(start)
cross apply (select CHARINDEX(' ',name.start)) _2nd(blank)
cross apply (
select left(name.start,
case when _2nd.blank = 0
then len(name.start)
else _2nd.blank -1
end)
) just(thename)
cross apply (
select PARSENAME(just.thename, 4) as ServerName
, PARSENAME(just.thename, 3) as DatabaseName
, PARSENAME(just.thename, 2) as SchemaName
, PARSENAME(just.thename, 1) as TableName
) _4part



Note that it doesn't handle the 4th row correctly. I'll leave that as an exercise for you
Go to Top of Page
   

- Advertisement -