1

I have table in which one of the column(info) contains below type of information, I want to run a query which check all the records and if it finds ""matched data"" value then remove the data.

Current value 
"{""value"":{""Volume ID"":""12345"",""matched data"":""eJzBFPafhF1xu0JdwH""}}"
Expected value 
"{""value"":{""Volume ID"":""12345"",""matched data"":"" ""}}"
8
  • 1
    What's your dbms? where is your sql? Commented Aug 1, 2018 at 19:34
  • Microsoft sql server 2014, table name is datacollection Commented Aug 1, 2018 at 19:41
  • So your colnum data is JSON? Commented Aug 1, 2018 at 19:47
  • yes its json value Commented Aug 1, 2018 at 19:54
  • @D-Shih can this be possible ? Commented Aug 1, 2018 at 20:15

2 Answers 2

1

If your Json format string is fixed. you can use CHARINDEX with SUBSTRING and REPLACE method

  1. get ""matched data"" string to be start index by CHARINDEX method
  2. use CHARINDEX to get }} to get the be the end index.
  3. SUBSTRING get contain ""matched data"" key value data.
  4. REPLACE to your expect format.

Final use Replace to get your expected result

TestDLL

CREATE TABLE T(
   Col1 VARCHAR(1000)
);

INSERT INTO T VALUES ('"{""value"":{""Volume ID"":""12345"",""matched data"":""eJzBFPafhF1xu0JdwH""}}"');
INSERT INTO T VALUES ('"{""value"":{""Volume ID"":""12345"",""matched data"":""test1213""}}"');

Query

SELECT REPLACE(Col1,SUBSTRING(Col1,CHARINDEX('""matched data"":', Col1) ,CHARINDEX('}}', Col1)  - CHARINDEX('""matched data"":', Col1)),'""matched data"":"" ""')
FROM T

sqlfiddle

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much . I salute to you for your smart approach and quick understanding which helped me to fix my problem.
0

idk what exactly do you need, but if you want to find out the solution to the if statement in sql your way is to use case.

Example:

CASE expression
    WHEN condition1 THEN result1
    WHEN condition2 THEN result2
   ...
    WHEN conditionN THEN resultN
    ELSE result
END

Example on a full query:

SELECT CustomerName, City, Country
FROM Customers
ORDER BY
(CASE
    WHEN City IS NULL THEN Country
    ELSE City
END);

3 Comments

thanks should i try something select info from datacollection ORDER BY (CASE WHEN ""matched data"" not sure now how to jump to next statement making value as null?
@gary it's a little bit hard for me to understand what exactly did you mean. You are asking about the situations when value is null?
If you see my current value and expected value . I need to run a query which should look for ""matched data"" and delete all the value which is in between next ""matched data"" :"" i want to delete all data from these codes""

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.