Skip to content
Advertisement

I need to do retrofit query using update or merge

I have two tables A and B. In A, I have a column called fetch_year. I need to consider table B from these two columns

  • primary_date
  • secondary_date

These columns have JSON values like {"lock":"true","date":"01/01/1990"} So from this, I need to get the date and I need to extract the year and should save it in table A column called fetch_year. Will always consider primary_date first then secondary_date(if primary_date is null)

The final result should be 1990 in the fetch_year column

Table A is empty as of now( only one column with cal_id)

Table B

In table B So I have n number of records in both the tables I need results like this in A table

In cal_id =4 in this case we have value in both columns so we are considering primary_date not secondary_date

Please help me with this problem

Advertisement

Answer

You could make use of either JSON_VALUE or OPENJSON here to extract the date from your JSON blobs.

I tend to prefer OPENJSON because it allows you to extract multiple values simultaneously and they don’t have to be at the same level in a nested JSON structure. With the “squirelly” dates in your example data, though, you may prefer the JSON_VALUE version with TRY_CONVERT so that you have more control over date deserialization.

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement