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 Wednesday, September 5, 2012 2:52 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 @ 5:45 PM
Points: 3,617, Visits: 5,237
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!
Post #1354385
Posted Wednesday, September 5, 2012 7:17 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 @ 3:37 PM
Points: 3,374, Visits: 7,300
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.
I am a great believer in luck, and I find the harder I work the more I have of it. Stephen Leacock

Forum Etiquette: How to post data/code on a forum to get the best help
Post #1354511
Posted Wednesday, September 5, 2012 8:17 AM


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:43 PM
Points: 736, Visits: 3,693
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
Post #1354570
Posted Wednesday, September 5, 2012 6:33 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 @ 5:45 PM
Points: 3,617, Visits: 5,237
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!
Post #1354969
« Prev Topic | Next Topic »

Add to briefcase ««12

Permissions Expand / Collapse