Database Research & Development

  • Home
  • NoSQL
    • NoSQL
    • Cassandra
  • Databases
    • Database Theory
    • Database Designing
    • SQL Server Coding Standards
    • SQL Server
    • PostgreSQL
    • MySQL
    • Greenplum
    • Linux
  • Interviews
    • SQL Server Interviews
    • MySQL Interviews
    • SQL Puzzles
  • DBA Scripts
    • SQL Server DBA Scripts
    • PostgreSQL DBA Scripts
    • MySQL DBA Scripts
    • Greenplum DBA Scripts
  • Home
  • Blog Archives !
  • (: Laugh@dbrnd :)
  • Contact Me !
sqlserverinterviews
Home 2016 December SQL Server 2016: Introduced JSON support, to store and retrieve JSON document

SQL Server 2016: Introduced JSON support, to store and retrieve JSON document

This article is half-done without your Comment! *** Please share your thoughts via Comment ***

JSON + RDBMS is one of the interesting topics, in the new version of SQL Server 2016 we can store JSON formatted data.

Now a day JSON data type is a standard feature for all new coming RDBMS product versions like PostgreSQL 9.4, and MySQL 5.7 have already introduced this.

Now We can say, we have SQL Server 2016 with the RDBMS + NoSQL concepts.
We can store JSON data as nvarchar, but the JSON related functions have the advantage of enforcing that each stored value is valid according to the JSON rules.

Small demonstration on SQL Server 2016 JSON:

Create a sample table and define two column as NVARCHAR to store JSON data:

1
2
3
4
5
6
7
8
CREATE TABLE tbl_TestSQLJSON
(
ID INTEGER PRIMARY KEY
,DepartmentName VARCHAR(250)
,EmployeeName NVARCHAR(MAX)
,EmployeeAddress NVARCHAR(MAX)
)
GO

Use ISJSON() and add check constraint in json columns:
If your json format and document is correct, It returns true other It returns false.

1
2
3
4
5
6
7
8
9
ALTER TABLE tbl_TestSQLJSON
ADD CONSTRAINT ck_tbl_TestSQLJSON_EmployeeName
CHECK (ISJSON( EmployeeName ) > 0)
GO
 
ALTER TABLE tbl_TestSQLJSON
ADD CONSTRAINT ck_tbl_TestSQLJSON_EmployeeAddress
CHECK (ISJSON( EmployeeAddress ) > 0)
GO

Insert few sample JSON data:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
INSERT INTO tbl_TestSQLJSON
VALUES
(
1
,'Sales'
,N'{"firstName": "Anvesh", "lastName": "Patel"}'
,N'
{"address" :
{
"India": "Hyderabad"
,"USA": "Newyork"
}
}'
)
,(
2
,'Production'
,N'{"firstName": "Neevan", "lastName": "Patel"}'
,N'
{"address" :
{
"India": "Ahmedabad"
,"USA": "Washington DC"
}
}'
)
,(
3
,'Animation'
,N'{"firstName": "Eric", "lastName": "Lorn"}'
,N'
{"address" :
{
"India": "Mumbai"
,"USA": "Chicago"
}
}'
)
GO

SELECT json data:

1
2
3
4
5
6
7
8
9
SELECT
ID
,DepartmentName
,JSON_VALUE(EmployeeName, '$.firstName') AS EmployeeFirstName
,JSON_VALUE(EmployeeAddress, '$.address.India') AS AddressIndia
,JSON_VALUE(EmployeeAddress, '$.address.USA') AS AddressUSA
,JSON_QUERY(EmployeeAddress, '$.address') AS EmployeeAddressJSON
FROM tbl_TestSQLJSON
GO

Use JSON_VALUE() in WHERE Clause:

1
2
3
4
5
6
7
SELECT
ID
,DepartmentName
,JSON_VALUE(EmployeeName, '$.firstName') AS EmployeeFirstName
FROM tbl_TestSQLJSON
WHERE JSON_VALUE(EmployeeName, '$.firstName') = 'Anvesh'
GO

Dec 10, 2016Anvesh Patel
SQL Server: Script to find waiting Queries which are block by other running QueriesSQL Server 2016: Introduced Query Store, to monitor the performance differences between query execution plans
Comments: 1
  1. marni
    April 10, 2017 at 7:52 am

    very simple and nice put up. thanks

Anvesh Patel
Anvesh Patel

Database Engineer

December 10, 2016 SQL ServerAnvesh Patel, database, database research and development, dbrnd, isjson, JSON, json_query, json_value, NoSQL, SQL Query, SQL Server, SQL Server Administrator, SQL Server Monitoring, SQL Server Performance Tunning, SQL Server Tips and Tricks, TSQL
About Me!

I'm Anvesh Patel, a Database Engineer certified by Oracle and IBM. I'm working as a Database Architect, Database Optimizer, Database Administrator, Database Developer. Providing the best articles and solutions for different problems in the best manner through my blogs is my passion. I have more than six years of experience with various RDBMS products like MSSQL Server, PostgreSQL, MySQL, Greenplum and currently learning and doing research on BIGData and NoSQL technology. -- Hyderabad, India.

About DBRND !

dbrnd

This is a personal blog (www.dbrnd.com).

Any views or opinions represented in this blog are personal and belong solely to the blog owner and do not represent those of people, institutions or organizations that the owner may or may not be associated with in professional or personal capacity, unless explicitly stated.

Feel free to challenge me, disagree with me, or tell me I’m completely nuts in the comments section of each blog entry, but I reserve the right to delete any comment for any reason whatsoever (abusive, profane, rude, or anonymous comments) - so keep it polite.

The content of this website is protected by copyright. No portion of this website may be copied or replicated in any form without the written consent of the website owner.

Recent Comments !
  • Anvesh Patel { Sure will do... } – May 27, 12:43 PM
  • Anvesh Patel { Great... } – May 27, 12:41 PM
  • Anvesh Patel { Great... } – May 27, 12:39 PM
  • Anvesh Patel { Great... } – May 27, 12:36 PM
  • Anvesh Patel { Great... } – May 27, 12:28 PM
  • Anvesh Patel { Great... } – May 27, 12:27 PM
  • Anvesh Patel { Great... } – May 27, 12:16 PM
  • Older »
Follow Me !
  • facebook
  • linkedin
  • twitter
  • youtube
  • google
  • flickr
© 2015 – 2019 All rights reserved. Database Research & Development (dbrnd.com)
Posting....