0

I am trying to find a certain set of characters in a column from a datatable. I have tried the pattern that seems more logical to me (right below) but it doesn't seem to be doing the job. What I wish to achieve is a pattern where I have something like '["5"]', basically with: square brackets, quotation marks, any integer number, quotation marks, square brackets. The output I am getting is just empty, and I can't seem to undersand why. Besides this, I would like to update the records that do not follow this pattern to follow it. Does anyone have a solution for this?

To give you some context, here is the test table:

enter image description here

I want to achive only the last three records. Here is what I have tried:

SELECT ToJsonTestValue
FROM   Test
WHERE  ToJsonTestValue LIKE '["%"]'

and

 UPDATE dbo.Test
SET     ToJsonTestValue = '["'+ToJsonTestValue+'"]'
WHERE ToJsonTestValue LIKE '#';
3
  • Square brackets need special processing... learn.microsoft.com/en-us/sql/t-sql/language-elements/… Commented Nov 10, 2020 at 14:32
  • Can your integer value exceed single digits e.g. ["937"]? Commented Nov 10, 2020 at 14:44
  • If this question is connected with your previous question, WHERE ISJSON(ToJsonTestValue) = 0 is also an option. Commented Nov 10, 2020 at 14:52

1 Answer 1

3

You have a couple of problem here. Firstly you have the square brackets, which needs escaping. Then you also use % which is a multi character wildcard, however, it appears that you want a single character. It also appears that that character can only be an integer, so you might want to be more specific. Either of these should give you the result you want:

--Using single character wildcard:
SELECT *
FROM (VALUES('["1"]'),('["["1"]"]'))V(S)
WHERE V.S LIKE '[[]"_"[\]]' ESCAPE '\';

--Specifically requiring int:
SELECT *
FROM (VALUES('["1"]'),('["["1"]"]'))V(S)
WHERE V.S LIKE '[[]"[0-9]"[\]]' ESCAPE '\';
Sign up to request clarification or add additional context in comments.

4 Comments

Yep that will do the trick! Also, is it possible to also update the remaining records? I mean, instead of just selecting them, the ones who do not follow the intended pattern be updated to follow it?
You can use that WHERE in an UPDATE, yes @MiguelFerreira . A WHERE doesn't care what type of statement it's in; SELECT,DELETE, UPDATE, etc.
What would you do for match any number of digits instead of only [0-9]? It's because I have been trying and not being able to solve this problem, the only way I did was by adding more [0-9] (with the "*" I wasn't able to), could you provide me an answer?
That's a new question at this stage, @MiguelFerreira . Moving the goal posts after getting an answer is frowned upon. Just make sure you reference this question and show your new attempts.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.