Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase 12»»

string manipulation - Need help ! Expand / Collapse
Author
Message
Posted Tuesday, September 4, 2012 1:35 PM


Right there with Babe

Right there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with Babe

Group: General Forum Members
Last Login: Today @ 1:44 PM
Points: 736, Visits: 3,730
Hi All,

Can anyone help me with below sql server 2005 and up:

input string:

BUILTIN\ADMINISTRATORS: [System Admin]YES;[Security Admin];[Server Admin];[setup Admin];[Process Admin];[Disk Admin]YES;[Database Creator]

output string:
BUILTIN\ADMINISTRATORS: [System Admin]YES;[Disk Admin]YES;

I am playing with Stuff and Replace functions but without any luck.



______________________________________________________________________________________________________________________________________________________________________________________
HTH !
Kin
MCTS : 2005, 2008
Active SQL Server Community Contributor
Post #1354145
Posted Tuesday, September 4, 2012 1:42 PM


Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Today @ 2:08 PM
Points: 3,563, Visits: 7,695
Can you explain more your requirements?
Because I'm sure that this won't work for you.
SELECT inputString, SUBSTRING( inputString, 1,42) + SUBSTRING( inputString, 105,16)
FROM (SELECT 'BUILTIN\ADMINISTRATORS: [System Admin]YES;[Security Admin];[Server Admin];[setup Admin];[Process Admin];[Disk Admin]YES;[Database Creator]' AS inputString) Input

However, it gives the correct output.



Luis C.
Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?

Forum Etiquette: How to post data/code on a forum to get the best help
Post #1354152
Posted Tuesday, September 4, 2012 1:53 PM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Sunday, August 3, 2014 3:41 PM
Points: 1,293, Visits: 1,430
Using one of Jeff Moden's splitter functions


DECLARE @pString VARCHAR(MAX) = 'BUILTIN\ADMINISTRATORS: [System Admin]YES;[Security Admin];[Server Admin];[setup Admin];[Process Admin];[Disk Admin]YES;[Database Creator];'
DECLARE @pDELIMITER CHAR(1) = ';'

;WITH SPLITS AS (
SELECT ItemNumber = ROW_NUMBER() OVER (ORDER BY t.N),
Item = SUBSTRING(@pString, t.N, CHARINDEX(@pDelimiter, @pString + @pDelimiter, t.N) - t.N)
FROM dbo.Tally as t
WHERE t.N <= DATALENGTH(@pString)+1 --DATATLENGTH allows for trailing space delimiters
AND SUBSTRING(@pDelimiter + @pString, t.N, 1) = @pDelimiter
)
SELECT Item
FROM SPLITS
WHERE ITEM Like '%YES'
ORDER BY ItemNumber


Then you just concatenate the resulting items with a delimiter, using STUFF, as you were originally experimenting with?
Post #1354158
Posted Tuesday, September 4, 2012 2:12 PM


Right there with Babe

Right there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with Babe

Group: General Forum Members
Last Login: Today @ 1:44 PM
Points: 736, Visits: 3,730
Luis Cazares (9/4/2012)
Can you explain more your requirements?
Because I'm sure that this won't work for you.
SELECT inputString, SUBSTRING( inputString, 1,42) + SUBSTRING( inputString, 105,16)
FROM (SELECT 'BUILTIN\ADMINISTRATORS: [System Admin]YES;[Security Admin];[Server Admin];[setup Admin];[Process Admin];[Disk Admin]YES;[Database Creator]' AS inputString) Input

However, it gives the correct output.


I am just writing a report that will show the login information with their privileges

NT AUTHORITY\SYSTEM:  [System Admin]YES;[Security Admin];[Server Admin];[setup Admin];[Process Admin];[Disk Admin];[Database Creator]
NT SERVICE\MSSQL$instance1: [System Admin]YES;[Security Admin];[Server Admin];[setup Admin];[Process Admin];[Disk Admin];[Database Creator]
NT SERVICE\SQLAGENT$instance1: [System Admin]YES;[Security Admin];[Server Admin];[setup Admin];[Process Admin];[Disk Admin];[Database Creator]YES

I want the result as below:
NT AUTHORITY\SYSTEM:  [System Admin]YES;
NT SERVICE\MSSQL$instance1: [System Admin]YES;
NT SERVICE\SQLAGENT$instance1: [System Admin]YES;[Database Creator]YES;

Thanks for you code .. but you code truncates the

SELECT inputString, SUBSTRING( inputString, 1,42) + SUBSTRING( inputString, 105,16)
FROM (SELECT 'NT SERVICE\SQLAGENT$instance1: [System Admin]YES;[Security Admin];[Server Admin]YES;[setup Admin];[Process Admin];[Disk Admin]YES;[Database Creator]YES' AS inputString) Input

NT SERVICE\SQLAGENT$instance1:  [System Adess Admin];[Disk



______________________________________________________________________________________________________________________________________________________________________________________
HTH !
Kin
MCTS : 2005, 2008
Active SQL Server Community Contributor
Post #1354168
Posted Tuesday, September 4, 2012 2:12 PM


Right there with Babe

Right there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with Babe

Group: General Forum Members
Last Login: Today @ 1:44 PM
Points: 736, Visits: 3,730
-- deleted double comments --

______________________________________________________________________________________________________________________________________________________________________________________
HTH !
Kin
MCTS : 2005, 2008
Active SQL Server Community Contributor
Post #1354169
Posted Tuesday, September 4, 2012 7:19 PM


Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Today @ 4:10 AM
Points: 3,634, Visits: 5,283
I agree that a string splitter combined with putting it all back together using FOR XML TYPE is the way to go:

DECLARE @pString VARCHAR(MAX) = 'BUILTIN\ADMINISTRATORS: [System Admin]YES;[Security Admin];[Server Admin];[setup Admin];[Process Admin];[Disk Admin]YES;[Database Creator];'

SELECT Result=
(SELECT item + CASE itemnumber WHEN 1 THEN ': ' ELSE ';' END
FROM dbo.DelimitedSplit8K(REPLACE(@pString, ': ', ';'), ';')
WHERE RIGHT(item, 3) = 'YES' OR itemnumber = 1
FOR XML PATH(''), TYPE).value('.', 'VARCHAR(MAX)')





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!
Post #1354292
Posted Wednesday, September 5, 2012 12:45 AM
SSC Eights!

SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!

Group: General Forum Members
Last Login: Today @ 1:04 AM
Points: 880, Visits: 670
pls try below code.
declare @t varchar(2000)='BUILTIN\ADMINISTRATORS: [System Admin]YES;[Security Admin];[Server Admin];[setup Admin];[Process Admin];[Disk Admin]YES;[Database Creator]'
select 'BUILTIN\ADMINISTRATORS: [System Admin]YES'+SUBSTRING(@t,PATINDEX('%Disk Admin]YES%',@t)-2,16)
Post #1354339
Posted Wednesday, September 5, 2012 1:34 AM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: 2 days ago @ 11:52 PM
Points: 1,126, Visits: 1,590
You can use a lot of methods to do this. It all depends on what part of the String you want to remove, the type of requirement(one time or a lot of times) and a lot more conditions. Apart from the abovew mentioned ways you can use Stuff and Replace as well as follows:

Declare @String Varchar(Max) = 'BUILTIN\ADMINISTRATORS: [System Admin]YES;[Security Admin];[Server Admin];[setup Admin];[Process Admin];[Disk Admin]YES;[Database Creator]'
Select STUFF(STUFF(@String, 42, 62, ''), 59, 18, '')
Select Replace(REPLACE(@String, '[Security Admin];[Server Admin];[setup Admin];[Process Admin];', ''), '[Database Creator]', '')

Now, you have a lot of options and you can choose the best depending on the conditions that define your requirement.

Edit: The Stuff Function should give better performance. And I am sure when Dwain sees the Replace functions he would definitely come up with a couple of expert performance points. That would clear the situation a little more.


Vinu Vijayan

For better and faster solutions please check..."How to post data/code on a forum to get the best help" - Jeff Moden
Post #1354353
Posted Wednesday, September 5, 2012 1:50 AM


Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Today @ 4:10 AM
Points: 3,634, Visits: 5,283
vinu512 (9/5/2012)
You can use a lot of methods to do this. It all depends on what part of the String you want to remove, the type of requirement(one time or a lot of times) and a lot more conditions. Apart from the abovew mentioned ways you can use Stuff and Replace as well as follows:

Declare @String Varchar(Max) = 'BUILTIN\ADMINISTRATORS: [System Admin]YES;[Security Admin];[Server Admin];[setup Admin];[Process Admin];[Disk Admin]YES;[Database Creator]'
Select STUFF(STUFF(@String, 42, 62, ''), 59, 18, '')
Select Replace(REPLACE(@String, '[Security Admin];[Server Admin];[setup Admin];[Process Admin];', ''), '[Database Creator]', '')

Now, you have a lot of options and you can choose the best depending on the conditions that define your requirement.

Edit: The Stuff Function should give better performance. And I am sure when Dwain sees the Replace functions he would definitely come up with a couple of expert performance points. That would clear the situation a little more.


I think what the OP wants to do is return any roles that have a YES next to them. My assumption was that any of the listed roles could be YES or ''. What if the string was:

'BUILTIN\ADMINISTRATORS: [System Admin];[Security Admin];[Server Admin]YES;[setup Admin];[Process Admin]YES;[Disk Admin];[Database Creator]YES'


What would your code return?

I don't think he was looking for something that is as specific as yours (but of course I could be wrong).

As to performance, applying a binary collation to @pString (my query) would be my only suggestion. Since he didn't provide a TABLE with a column of such strings to parse, I wasn't aiming for high performance.



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!
Post #1354366
Posted Wednesday, September 5, 2012 2:35 AM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: 2 days ago @ 11:52 PM
Points: 1,126, Visits: 1,590
ohhh!!!! yes!!....i didn't look at the logic that way...that completely missed my eye. Sorry, My Bad. I was in a hurry, I just saw the OP's post and replied without reading any of the other posts. May be if I had read the other posts then I would have had a better idea of the OP's requirement.

Vinu Vijayan

For better and faster solutions please check..."How to post data/code on a forum to get the best help" - Jeff Moden
Post #1354379
« Prev Topic | Next Topic »

Add to briefcase 12»»

Permissions Expand / Collapse