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


help with substring


help with substring

Author
Message
mxy
mxy
SSCrazy
SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)

Group: General Forum Members
Points: 2098 Visits: 4329
hello all,

need help to split data as below. i try to use charindex to find occurrence of 'And Id in (', but facing difficulty with close parenthesis due to multiple occurrences.

DECLARE @test NVARCHAR(4000)

SET @test =
N'((User = ''active'') And Id in ( 1, 2, 3) And userstate=''CT'')'

SELECT @test


expected o/p
((User = 'active') And userstate='CT'), And Id in ( 1, 2, 3)
Jason A. Long
Jason A. Long
SSChampion
SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)

Group: General Forum Members
Points: 10122 Visits: 5995
mxy - Thursday, February 8, 2018 3:54 PM
hello all,

need help to split data as below. i try to use charindex to find occurrence of 'And Id in (', but facing difficulty with close parenthesis due to multiple occurrences.

DECLARE @test NVARCHAR(4000)

SET @test =
N'((User = ''active'') And Id in ( 1, 2, 3) And userstate=''CT'')'

SELECT @test


expected o/p
((User = 'active') And userstate='CT'), And Id in ( 1, 2, 3)

#1 Why? What possible reason could you have for wanting to do this?
#2 If you're doing what it looks like you're doing... DON'T!!! You're setting yourself up for a massive SQL injection vulnerability.

mxy
mxy
SSCrazy
SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)

Group: General Forum Members
Points: 2098 Visits: 4329
Jason A. Long - Thursday, February 8, 2018 4:13 PM
mxy - Thursday, February 8, 2018 3:54 PM
hello all,

need help to split data as below. i try to use charindex to find occurrence of 'And Id in (', but facing difficulty with close parenthesis due to multiple occurrences.

DECLARE @test NVARCHAR(4000)

SET @test =
N'((User = ''active'') And Id in ( 1, 2, 3) And userstate=''CT'')'

SELECT @test


expected o/p
((User = 'active') And userstate='CT'), And Id in ( 1, 2, 3)

#1 Why? What possible reason could you have for wanting to do this?
#2 If you're doing what it looks like you're doing... DON'T!!! You're setting yourself up for a massive SQL injection vulnerability.

i have app that sends dynamic filter statement (app is almost 10 years old), in clause is causing the problem where query takes over 5 mins. i was able to tune by removing in clause and adding a join. i use custom sql injection function(checks drop , create, exec etc) to make sure nothing is injected from the front end.

Jason A. Long
Jason A. Long
SSChampion
SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)

Group: General Forum Members
Points: 10122 Visits: 5995
mxy - Thursday, February 8, 2018 4:21 PM
Jason A. Long - Thursday, February 8, 2018 4:13 PM
mxy - Thursday, February 8, 2018 3:54 PM
hello all,

need help to split data as below. i try to use charindex to find occurrence of 'And Id in (', but facing difficulty with close parenthesis due to multiple occurrences.

DECLARE @test NVARCHAR(4000)

SET @test =
N'((User = ''active'') And Id in ( 1, 2, 3) And userstate=''CT'')'

SELECT @test


expected o/p
((User = 'active') And userstate='CT'), And Id in ( 1, 2, 3)

#1 Why? What possible reason could you have for wanting to do this?
#2 If you're doing what it looks like you're doing... DON'T!!! You're setting yourself up for a massive SQL injection vulnerability.

i have app that sends dynamic filter statement (app is almost 10 years old), in clause is causing the problem where query takes over 5 mins. i was able to tune by removing in clause and adding a join. i use custom sql injection function(checks drop , create, exec etc) to make sure nothing is injected from the front end.

All I can say is be careful... I used to think you could protect against SQL injection using that method myself... That is until I saw a demonstration using VARBINARY hex string that breezed right through the black list filter.
If your app is exposed to the outside world, and you're relying on a black list filter, you're vulnerable... period... full stop.

Going back to the initial request... You're expected output is exactly the same as the input... all you did was swap the 2nd & 3rd predicate. That isn't going to impact query performance in the slightest.
That said, it should be easy enough to parse the string into rows, using " AND " as a 5 character delimiter... remove the offending row and then reassemble the string.

mxy
mxy
SSCrazy
SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)

Group: General Forum Members
Points: 2098 Visits: 4329
thanks, i do have a scenario where there will be combination 'or', 'and'.
if it is only and clause i can split data easily.

DECLARE @test NVARCHAR(4000)

SET @test =
N'((User = ''active'') And Id in ( 1, 2, 3) or userstate=''CT'')'

SELECT @test

Jason A. Long
Jason A. Long
SSChampion
SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)

Group: General Forum Members
Points: 10122 Visits: 5995
mxy - Thursday, February 8, 2018 4:59 PM
thanks, i do have a scenario where there will be combination 'or', 'and'.
if it is only and clause i can split data easily.

DECLARE @test NVARCHAR(4000)

SET @test =
N'((User = ''active'') And Id in ( 1, 2, 3) or userstate=''CT'')'

SELECT @test

Just split on the ANDs... Logically, it wouldn't make sense to split ORs... At least I can't think of a valid use case.

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