Recent PostsRecent Posts Popular TopicsPopular Topics
 Home Search Members Calendar Who's On

 string manipulation - Need help ! Rate Topic Display Mode Topic Options
Author
 Message
 Posted Wednesday, September 5, 2012 2:52 AM
 Hall of Fame Group: General Forum Members Last Login: Wednesday, February 24, 2016 6:28 AM Points: 3,977, Visits: 6,431
 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
Post #1354385
 Posted Wednesday, September 5, 2012 7:17 AM
 SSCrazy Eights Group: General Forum Members Last Login: Today @ 7:27 AM Points: 8,205, Visits: 17,755
 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 '' ENDFROM @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
Post #1354511
 Posted Wednesday, September 5, 2012 8:17 AM
 Right there with Babe Group: General Forum Members Last Login: Monday, October 10, 2016 2:46 PM Points: 744, Visits: 4,306
 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 '' ENDFROM @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 !KinMCTS : 2005, 2008Active SQL Server Community Contributor
Post #1354570
 Posted Wednesday, September 5, 2012 6:33 PM
 Hall of Fame Group: General Forum Members Last Login: Wednesday, February 24, 2016 6:28 AM Points: 3,977, Visits: 6,431
 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
Post #1354969

 Permissions