I am working on a table that stores JSON as:
Now, I am looking to flatten this data in SQL to something like this:
I tried using OPENJSON but not quite able to make it work. Any help would be highly appreciated!
Got it working! Thanks!
SELECT Rollno,
t.Value AS Subject
from [Students]
CROSS APPLY OPENJSON(Subjects, '$.subjects') t
Related
I need some help with this Hive Query please.
Table name is frame_curated
Column name is Message
Column type is string
In that column the data is formatted like this:
IP":"1735", "ID":"G54X"
I'm looking for records where ID="G54X" in column Message.
Something like this
Select * FROM frame_curated WHERE frame_curated.Message LIKE '%G54X%'
From my research, I think it should look like this:
Select * FROM frame_curated WHERE frame_curated.Message.ID ['G54X']
But its not working.
Your help is sincerely appreciated.
Basil
I don't think frame_curated.Message.ID ['G54X'] would work for string data type. it could only work if your data type is List<dict<string:string>>
you can use something like this
select * from temp.test2 where value like '%"ID":%G54X%'
The other approach is, you change the table datatype to complex and run the query you suggested.
I have a something like this in my table column:
{"InputDirection":0,"Mask":"AA","FormatString":null,"AutoCompleteValue":null,
"Filtered":"0123456789","AutoComplete":false,"ReadOnly":true}
What I want to do is to change A to N in "Mask":"AA" and remove "Filtered":"0123456789" if they exist. Mask could be in different forms like A9A, 'AAAA`, etc.
If it was in C# I could do it by myself by parsing it to JSON, etc but I need to do it within SQL.
I've found this article which shows how to parse JSON to Table. This gave me an idea that I can parse each field to temp table and make the changes on that and convert it back to JSON so update the actual field where I take this JSON field from. However, this looks like a cumbersome process for both me and the server.
Any better ideas?
You can use this LINK .
And then use the following code
select * into #demo from
(Select * from parseJSON('{"InputDirection":0,"Mask":"AA","FormatString":null,"AutoCompleteValue":null,
"Filtered":"0123456789","AutoComplete":false,"ReadOnly":true}
')) a
select * from #demo
--- CHANGE THE DATA HERE AS REQUIRED
DECLARE #MyHierarchy JSONHierarchy;
INSERT INTO #myHierarchy
select * from #demo;
-- USE THIS VALUE AND UPDATE YOUR JSON COLUMN
SELECT dbo.ToJSON(#MyHierarchy)
drop table #demo
I may be getting something wrong here but why can’t you simply use REPLACE to update what’s needed and LIKE to identify JSON strings that should be updated?
update table_T
set json_string = REPLACE(json_string, '"Filtered":"0123456789",', '')
where json_string like '%"Mask":"AA"%'
Not sure I understand why do you need to parse it….
I have a massive view and using a SQL query I want to produce output as XML.
So if I query SQL it gives me back XML.
I have no idea how to do it so please point me in right direction.
SADLY it's for SQL Server 2000
Use FOR XML RAW It returns XML.
Like:
SELECT * FROM table_name FOR XML RAW;
I'm trying to create a stored procedure in sql 2008 to select the id values from some xml like this.
DECLARE #idPolygonXML XML
SET #idPolygonXML =
'<polygons>
<id>35</id>
<id>36</id>
<id>37</id>
<id>38</id>
<id>39</id>
<id>40</id>
</polygons>'
I can get the id for a specific index but I need all of them and I keep getting 'value() requires a singleton'.
Does anyone know how I can get all these values without changing the xml?
Take a look at this: http://msdn.microsoft.com/en-us/library/ms188282.aspx
This isn't tested, but your query would look something like this
SELECT T2.ID.query('.')
CROSS APPLY #idPolygonXML.nodes('/polygons/id') as T2(ID)
Actually both tables are the same, and I just need to merge data. Problem is that one column is defined with XML shema, which is same in both tables, and for my query I am getting this error from sql server studio:
"Implicit conversion between XML types constrained by different XML schema collections is not allowed. Use the CONVERT function to run this query."
Help me writedown this query.
I have something like this:
INSERT INTO table1
SELECT * FROM table2
WHERE id NOT IN (select id from table1);
Without more info on your table structure and the xml schemas I'm not sure how much assistance I can be. That said there's an article that discusses this exact problem here
http://sqlblogcasts.com/blogs/martinbell/archive/2010/11/08/Using-XML-Schemas.aspx
And his example of using the convert statement to overcome exactly this problem is as follows.
INSERT INTO [dbo].[Test_ProductModel_Content]( [CatalogDescription] )
SELECT CONVERT(XML, [CatalogDescription] )
FROM AdventureWorks2008.Production.ProductModel
WHERE [CatalogDescription] IS NOT NULL ;
GO
Hope that helps, if not post more information and I'm sure someone can help you out.