- SQL Tutorial
- SQL - Home
- SQL - Overview
- SQL - RDBMS Concepts
- SQL - Databases
- SQL - Syntax
- SQL - Data Types
- SQL - Operators
- SQL - Expressions
- SQL Database
- SQL - Create Database
- SQL - Drop Database
- SQL - Select Database
- SQL - Rename Database
- SQL - Show Databases
- SQL - Backup Database
- SQL Table
- SQL - Create Table
- SQL - Show Tables
- SQL - Rename Table
- SQL - Truncate Table
- SQL - Clone Tables
- SQL - Temporary Tables
- SQL - Alter Tables
- SQL - Drop Table
- SQL - Delete Table
- SQL - Constraints
- SQL Queries
- SQL - Insert Query
- SQL - Select Query
- SQL - Select Into
- SQL - Insert Into Select
- SQL - Update Query
- SQL - Delete Query
- SQL - Sorting Results
- SQL Views
- SQL - Create Views
- SQL - Update Views
- SQL - Drop Views
- SQL - Rename Views
- SQL Operators and Clauses
- SQL - Where Clause
- SQL - Top Clause
- SQL - Distinct Clause
- SQL - Order By Clause
- SQL - Group By Clause
- SQL - Having Clause
- SQL - AND & OR
- SQL - BOOLEAN (BIT) Operator
- SQL - LIKE Operator
- SQL - IN Operator
- SQL - ANY, ALL Operators
- SQL - EXISTS Operator
- SQL - CASE
- SQL - NOT Operator
- SQL - NOT EQUAL
- SQL - IS NULL
- SQL - IS NOT NULL
- SQL - NOT NULL
- SQL - BETWEEN Operator
- SQL - UNION Operator
- SQL - UNION vs UNION ALL
- SQL - INTERSECT Operator
- SQL - EXCEPT Operator
- SQL - Aliases
- SQL Joins
- SQL - Using Joins
- SQL - Inner Join
- SQL - Left Join
- SQL - Right Join
- SQL - Cross Join
- SQL - Full Join
- SQL - Self Join
- SQL - Delete Join
- SQL - Update Join
- SQL - Left Join vs Right Join
- SQL - Union vs Join
- SQL Keys
- SQL - Unique Key
- SQL - Primary Key
- SQL - Foreign Key
- SQL - Composite Key
- SQL - Alternate Key
- SQL Indexes
- SQL - Indexes
- SQL - Create Index
- SQL - Drop Index
- SQL - Show Indexes
- SQL - Unique Index
- SQL - Clustered Index
- SQL - Non-Clustered Index
- Advanced SQL
- SQL - Wildcards
- SQL - Comments
- SQL - Injection
- SQL - Hosting
- SQL - Min & Max
- SQL - Null Functions
- SQL - Check Constraint
- SQL - Default Constraint
- SQL - Stored Procedures
- SQL - NULL Values
- SQL - Transactions
- SQL - Sub Queries
- SQL - Handling Duplicates
- SQL - Using Sequences
- SQL - Auto Increment
- SQL - Date & Time
- SQL - Cursors
- SQL - Common Table Expression
- SQL - Group By vs Order By
- SQL - IN vs EXISTS
- SQL - Database Tuning
- SQL Function Reference
- SQL - Date Functions
- SQL - String Functions
- SQL - Aggregate Functions
- SQL - Numeric Functions
- SQL - Text & Image Functions
- SQL - Statistical Functions
- SQL - Logical Functions
- SQL - Cursor Functions
- SQL - JSON Functions
- SQL - Conversion Functions
- SQL - Datatype Functions
- SQL Useful Resources
- SQL - Questions and Answers
- SQL - Quick Guide
- SQL - Useful Functions
- SQL - Useful Resources
- SQL - Discussion
SQL - JSON_ARRAY() Function
The SQL JSON_ARRAY() function takes an expression or a comma-separated list of expressions and returns a JSON array containing those values. Several types of select-items may be used in conjunction with JSON ARRAY in a SELECT statement. Other places where a SQL function may be used, such as a WHERE clause, allow the specification of JSON ARRAY.
Each array element's value is returned by JSON ARRAY as either a string (with double quotes around it) or a number. The format of returned numbers is canonical. As a literal, a numeric string surrounded in double quotations is returned. The return value for all other data types, such as Date and $List, is a string. JSON_ARRAY does not support the COUNT(*) aggregate function.
Syntax
Following is the syntax of the SQL JSON_ARRAY() function −
JSON_ARRAY ( [ <json_array_value> [,...n] ] [ <json_null_clause> ] ) <json_array_value> ::= value_expression <json_null_clause> ::= NULL ON NULL | ABSENT ON NULL
Parameters
json_array_value − It is an expression that indicates the value of the element in the JSON array.
json_null_clause − It is used to control the behaviour of the JSON_OBJECT function when the value expression is NULL. The option NULL ON NULL converts SQL NULL values into JSON NULL when generating the value of JSON_ARRAY. The option ABSENT ON NULL will omit the element from the JSON array if its value is NULL. The default setting for this option is "Absent on Null."
Example
Let's retrive the empty JSON ARRAY by using the following query −
SELECT JSON_ARRAY() AS Result;
Output
When we execute the above query, the output is obtained as follows −
+------------------------------------+ | Result| +------------------------------------+ | [] | +------------------------------------+
Example
Let's retrive all the JSON_ARRAY values that was passed in the expression by using the following query −
SELECT JSON_ARRAY('abc', 14, 'bcd', 3) AS Result;
Output
When we execute the above query, the output is obtained as follows −
+------------------------------------+ | Result| +------------------------------------+ | ["abc",14,"bcd",3] | +------------------------------------+
Example
Let's look at another example where we are going to use NULL as input values, which negates the null values, and return other input value in JSON_ARRAY. Because the json_null_clause is omitted and the default for this is ABSENT ON NULL, Hence the NULL value is not converted to a JSON null value −
SELECT JSON_ARRAY('abc', 14, 'bcd', NULL) AS Result;
Output
When we execute the above query, the output is obtained as follows −
+------------------------------------+ | Result| +------------------------------------+ | ["abc",14,"bcd"] | +------------------------------------+
Example
Let's consider another scenario where we are going to pass NULL ON NULL option which helps to convert any SQL NULL value to JSON null value and retrieving the output by using the following query−
SELECT JSON_ARRAY('aBC', 14, NULL, 3 NULL ON NULL) AS Result;
Output
On executing the above query, the output is displayed as follows −
+------------------------------------+ | Result| +------------------------------------+ | ["aBC",14,null,3] | +------------------------------------+
Example
Let's consider another example, where we are going to retrive the JSON_ARRAY with two element containing JSON string and another element is JSON object by using the following query −
SELECT JSON_ARRAY('ABC', JSON_OBJECT('car':'RX100', 'type':2)) AS Result;
Output
On executing the above query, the output is displayed as follows −
+------------------------------------+ | Result| +------------------------------------+ | ["ABC",{"car":"RX100","type":2}] | +------------------------------------+
Example
Considering the another scenario, where we are going to retrive the JSON_ARRAY with three elements containing JSON string, JSON object and another element contains JSON array by using the following query −
SELECT JSON_ARRAY('TP', JSON_OBJECT('course':'HTML', 'type':2), JSON_ARRAY(145, null, 6 NULL ON NULL)) AS Result;
Output
On executing the above query, the output is displayed as follows −
+----------------------------------------------+ | Result | +----------------------------------------------+ |["TP",{"course":"HTML","type":2},[145,null,6]]| +----------------------------------------------+
Example
Let's look into the following example, where we are going to specify the input as variable or SQL exression by using the following the query −
DECLARE @work varchar(64) = NEWID(); SELECT JSON_ARRAY(1, @work, (SELECT @@SPID)) AS Result;
Output
The output for the above query is produced as given below −
+----------------------------------------------+ | Result | +----------------------------------------------+ |[1,"1DAC88DD-BE4E-45C7-BEA1-6FAF3EE86EE0",73] | +----------------------------------------------+
To Continue Learning Please Login
Login with Google