help with substring

  • 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-2 NVARCHAR(4000)

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

    SELECT @test-2

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

  • 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-2 NVARCHAR(4000)

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

    SELECT @test-2

    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.

  • 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-2 NVARCHAR(4000)

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

    SELECT @test-2

    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.

  • 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-2 NVARCHAR(4000)

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

    SELECT @test-2

    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.

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

Viewing 6 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic. Login to reply