SQL Geting json data value from row to another colum - sql

I have coupule of columns in my table and one of them is a CLOB with json object.
I am working on data extraction mechanism from table and i was wondering if it is possible to create a new view with a new column containing certain value from that json (for example one column have rows with data like ...,"request":{"status":"open",.....} and i want new column STATUS)
Do you have any ideas how could I achieve this?

You can use JSON_VALUE.
SELECT
JSON_VALUE(jsonInfo,'$.request.status') status
FROM
( VALUES('{"request":{"status":"open"}}') ) J(jsonInfo)
Result:
status
------------
open

Related

SQL query to get a value from XML in one table and update a column in another table with that value

I have got two tables, Table A containing my subject that needs to be updated and Table B that contains XML used to populate the columns in Table A.
A new column has been added to Table A which relates to a specific piece of in the XML.
Any ideas how I can query the XML and insert the into the new column?
UPDATE:
I have tried to query the XML using
select
xml
from
TableB
where
xml.value('(/path/value)[1]','string') like 'VALUE'
However when I do this, "xml.value" is highlghted as an error "Cannot find either column "xml" or the user-defined function or aggregate "xml.value", or the name in ambiguous"

how to validate data before we do data mapping or migration

I have task where i need to move data from one table1 to table2, i have source columns and destination columns but before i move data is it possible to find out is there any data in source column doesn't not fit destination column because of destination field type difference or field length is less like that ?
Like in a scenario i have source column which is char(1) and i am trying to map that column data to int column, here most of rows in source column have integer values but there are some alpha characters in some rows so i want to find out all those rows which are going to fail....the logic should work for all data types.
Thanks in advance!!
Use this code find the columns which are different(datatype) between New table and Old table.
SELECT TABLE_SCHEMA,COLUMN_NAME,IS_NULLABLE,DATA_TYPE,
CHARACTER_MAXIMUM_LENGTH,NUMERIC_PRECISION,NUMERIC_SCALE
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = 'New_Table' AND TABLE_SCHEMA='Table_Schema'
EXCEPT
SELECT TABLE_SCHEMA,COLUMN_NAME,IS_NULLABLE,DATA_TYPE,
CHARACTER_MAXIMUM_LENGTH,NUMERIC_PRECISION,NUMERIC_SCALE
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = 'Old_Table' AND TABLE_SCHEMA='Table_Schema'

Copy Contents of One Column Of a Table To another of a different Table SQL

I want to copy the content of one column in table A and replace the contents (not insert into it - the number of rows will be the same) of another column in another table.
I can't a where condition, the table has only just been created at this point with one empty timestamp column. it will be populated via pyodbc class after the timestamps have been added - this query will fill the timestamps for me
What is the SQL command for this?
Thanks!
After discussion, this is the query needed : INSERT INTO OCAT_test_table (DateTimeStamp) SELECT DateTimeStamp FROM DunbarGen

sql dump of data based on selection criteria

When extracting data from a table (schema and data) I can do this by right clicking on the database and by going to tasks->Generate Scripts and it gives me all the data from the table including the create script, which is good.
This though gives me all the data from the table - can this be changed to give me only some of the data from the table? e.g only data on the table after a certain dtmTimeStamp?
Thanks,
I would recommend extracting your data into a separate table using a query and then using generate scripts on this table. Alternatively you can extract the data separately into a flatfile using the export data wizard (include your column headers and use comma seperators with double quote field delimiters).
To make a copy of your table:
SELECT Col1 ,Col2
INTO CloneTable
FROM MyTable
WHERE Col3 = #Condition
(Thanks to #MarkD for adding that)

Get values based on newly inserted value using SQL

I want to make filtration on a column after selecting a specific value of another column in the same table, I tried to use #... special character followed by the column's name to get the address of this value.
My SQL statement is like the following :
SELECT ATTRIBUTE FROM TABLE WHERE FIELD = '#FIELDNAME';
If I used a specific value instead of #FIELDNAME, it will work properly but it will be static but I need it to be dynamic based on the selected value.
Create another table which will have the list of values that are in the FIELDNAME and give each record a unique id ,then retrieve the value depending on what you have selected by the name of the new table's field preceded by '#...'
I don't know if that what are you looking for, please let me know.
If no triggers are allowed, do you have any date/time column in the table? Is it possible to have that extra column anyway to see the time of a newly inserted row?
You may have to check the lastest row entered, save its field value into a variable. Then do the select based on the variable value.
Based on the vague last row id you could try the following (it's not pretty). But again, if you have date/time that's more accurate.
select attribute from table
where field = (select field from table
where rowid =(select max(rowid) from table))
;
upate
Do you have the priviledge to set up your insert command as below:
insert into table (id, col1, col2,...) values (1,'something', 'something',...)
returning id into variable; -- you may either save field or id depending on your table
Then you may use this variable to select the records you want.