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


string manipulation - Need help !


string manipulation - Need help !

Author
Message
dwain.c
dwain.c
SSCoach
SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)

Group: General Forum Members
Points: 18467 Visits: 6431
vinu512 (9/5/2012)
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.


I think you aren't the only one that's replied to this thread that did the same. No harm done. :-)

We'll need to hear back from the OP to find out for sure. Hopefully we will.


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
Luis Cazares
Luis Cazares
SSC-Forever
SSC-Forever (43K reputation)SSC-Forever (43K reputation)SSC-Forever (43K reputation)SSC-Forever (43K reputation)SSC-Forever (43K reputation)SSC-Forever (43K reputation)SSC-Forever (43K reputation)SSC-Forever (43K reputation)

Group: General Forum Members
Points: 43352 Visits: 19859
It might not be the optimal way, but it's simple enough for everyone to understand. However, I haven't tested for performance against other methods.


DECLARE @Test Table(
MyString varchar(MAX))
INSERT @Test VALUES
('NT AUTHORITY\SYSTEM: [System Admin]YES;[Security Admin];[Server Admin]YES;[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')

SELECT LEFT( MyString, CHARINDEX( ':', MyString) + 1) +
CASE WHEN CHARINDEX( 'System Admin]YES;', MyString) > 0
THEN '[System Admin]YES;' ELSE '' END +
CASE WHEN CHARINDEX( 'Security Admin]YES;', MyString) > 0
THEN '[Security Admin]YES;' ELSE '' END +
CASE WHEN CHARINDEX( 'Server Admin]YES;', MyString) > 0
THEN '[Server Admin]YES;' ELSE '' END +
CASE WHEN CHARINDEX( 'setup Admin]YES;', MyString) > 0
THEN '[setup Admin]YES;' ELSE '' END +
CASE WHEN CHARINDEX( 'Process Admin]YES;', MyString) > 0
THEN '[Process Admin]YES;' ELSE '' END +
CASE WHEN CHARINDEX( 'Disk Admin]YES;', MyString) > 0
THEN '[Disk Admin]YES;' ELSE '' END +
CASE WHEN CHARINDEX( 'Database Creator]YES', MyString) > 0
THEN '[Database Creator]YES;' ELSE '' END
FROM @Test




Luis C.
General Disclaimer:
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?


How to post data/code on a forum to get the best help: Option 1 / Option 2
SQLQuest29
SQLQuest29
SSCrazy
SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)

Group: General Forum Members
Points: 2277 Visits: 4369
Luis Cazares (9/5/2012)
It might not be the optimal way, but it's simple enough for everyone to understand. However, I haven't tested for performance against other methods.


DECLARE @Test Table(
MyString varchar(MAX))
INSERT @Test VALUES
('NT AUTHORITY\SYSTEM: [System Admin]YES;[Security Admin];[Server Admin]YES;[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')

SELECT LEFT( MyString, CHARINDEX( ':', MyString) + 1) +
CASE WHEN CHARINDEX( 'System Admin]YES;', MyString) > 0
THEN '[System Admin]YES;' ELSE '' END +
CASE WHEN CHARINDEX( 'Security Admin]YES;', MyString) > 0
THEN '[Security Admin]YES;' ELSE '' END +
CASE WHEN CHARINDEX( 'Server Admin]YES;', MyString) > 0
THEN '[Server Admin]YES;' ELSE '' END +
CASE WHEN CHARINDEX( 'setup Admin]YES;', MyString) > 0
THEN '[setup Admin]YES;' ELSE '' END +
CASE WHEN CHARINDEX( 'Process Admin]YES;', MyString) > 0
THEN '[Process Admin]YES;' ELSE '' END +
CASE WHEN CHARINDEX( 'Disk Admin]YES;', MyString) > 0
THEN '[Disk Admin]YES;' ELSE '' END +
CASE WHEN CHARINDEX( 'Database Creator]YES', MyString) > 0
THEN '[Database Creator]YES;' ELSE '' END
FROM @Test



Thanks for all your replies..

This is what I did:

get the results of the query in a temp table and then followed what @Luis suggested.

I learned a lot of different way to achieve the same results.

I did not chose for the tally table or the splitter function as I did not want to create extra table or function to achieve what could be done using the results from temp table.

I might be overlooking some obvious benefits which Jeff has an article on SSC, but currently it is not an option for me. I have used Tally and Number tables a lot and they have benefited me ..

Thanks to all who provided different methods/solutions.

Good learning ...

______________________________________________________________________________________________________________________________________________________________________________________
HTH !
Kin
MCTS : 2005, 2008
Active SQL Server Community Contributor :-)
dwain.c
dwain.c
SSCoach
SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)

Group: General Forum Members
Points: 18467 Visits: 6431
So in other words, the string always contains the same roles (or a subset of them) and the ordering of the output result is always fixed and not based on the order of the input roles.

Right?


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
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