I want to query the content of the json inside column like below - json

I am saving JSON data my SQL server 2016 table and I want to query the table by applying where clause on the JSON like below.
select c.customer_details.name.fullName
from j_customer c
where c.customer_details.name.fullName like '%Gopi%';
this is possible in oracle but in mssql it gives the error like below
Cannot call methods on nvarchar(max).

After going thru internet finally I found out that there is a method called JSON_VALUE() which should be used for finding the content of the json. The syntax for mssql is like this.
select *
from j_customer where JSON_VALUE(c.customer_details, '$.name.fullName') = 'C';

As far as I understood it, you have stored your JSON data into the table using a query like below
declare #json nvarchar(max) = '"Customer_details":{"name":{"fullName":"Dhruv Joshi"}}'
INSERT INTO j_customer
SELECT *
FROM OPENJSON(#json)
WITH (---some columns
fullName nvarchar(max) '$.Customer_details.name.fullName'
)
In such cases you can simply query your table like below
select c.fullName
from j_customer c
where c.fullName like '%Gopi%';
In SQL the fully qualified SQL column name usually is like [DatabaseName].[schema].[tablename].[columnname], so here in WHERE clause SQL interprets c.customer_details.name.fullName as c.customer_details a column as c is a table alias. And then name.fullname looks like a method call on the column name which generates the error.

Related

How to convert IF or nested If statement like EXCEL to dynamic SQL Query in SQL server

I have came across one pretty problem in which I need to convert the nested if statement (like we used to write in excel) to dynamic SQL query.
I want to convert this
select 'IIF(A>B,A,IIF(B>C,B,C))'
to like this
select 'CASE WHEN A>B THEN A ELSE CASE WHEN B > C THEN B ELSE C END END'.
Why I am doing this I have formula column in the table user can insert formula in that and I need to convert this to SQL statement.
Do anyone has idea how can I do that?
The above is for only example I will be having 150 rows for different formula's entered by user.
Note : Here do not interpret IIF as SQL server IIF, it can be IF.
Assuming that A,B,C are numbers we can do something like this
SELECT
(
Select MAX(cols)
from
(VALUES (ColA), (ColB), (ColC))tbl(cols)
) as greatest
from yourtable

How to use Oracle sql with json_object to generate json in NiFi

I am trying to use Oracle JSON_OBJECT to write a query to generate a specific json format.
Here is a sample query (the real query is more complex):
SELECT JSON_OBJECT('name' value name)
FROM table_a
WHERE name = 'John'
The query is working in Oracle. I used ExecuteSQL processor and put this query in it. It shows
illegal character in JSON_OBJECT('name' value name)
Any suggestions?
You most likely need to give the column an alias. Nifi probably doesn't like having spaces, single quotes, or parenthesis in the column name.
select json_object('name' value NAME) as json_with_name
from table_a
where NAME = 'John';

Assigning multi value parameter (values) to a local parameter in sql query

I have a situation where I need to store the multi value parameter to a local parameter in sql query.
In the SSRS report builder I have created two multi value parameters #Customer and #LogisticsGroup
And in my SQL query , I have to assign those values to local parameters something like this
DECLARE #Acct NVARCHAR(100) , #LgstGroup NVARCHAR(MAX)
SELECT #Acct = (#Customer)
,#LgstGroup = (#LogisticsGroup)
But with this kind of approach I'm able to select only one value , if I select two values then the query is failing.
I tried this , but it seems like incorrect syntax.
DECLARE #Acct NVARCHAR(100) , #LgstGroup NVARCHAR(MAX)
SELECT #Acct IN (#Customer)
,#LgstGroup IN (#LogisticsGroup)
Please help to resolve this issue. Thanks much
You can store delimited values in a parameter. So for example, #Customer might have a string of ID's in it like 1,2,3. The report can concatenate the multiple values into this format using the Join function.
Now, in the SQL, one option is to use the LIKE operator to search the string. You could write something like:
#Customer like '%,' + Column_Name + ',%'
However, this approach is inefficient and you have to be careful of partial value matches.
A better approach is to create a user-defined table-valued function that can split the values and treat them like a table. There are plenty of examples out there, it's a pretty simple function. Then in practice it would look like this:
WHERE Column_Name in (select * from Split(#Customer))
OR
INNER JOIN Split(#Customer) ON...

How to check in SELECT statement if column exists before showing it

I use Access 97. In select statement i would like to check if column, which I would like to be shown, exists in my tabe. If it is not it should not be shown in the result, or it can be fil with 0/NULL etc.
The following code in SQL Server looks like:
IF NOT EXISTS(SELECT *
FROM sys.columns
WHERE [name] = N'Minuta'
AND [object_id] = OBJECT_ID(N'DokFin'))
BEGIN
SELECT Godzina from DokFin
END
but how can I achieve this in MS Access 97?
Access doesn't have full system dictionaries like in MS SQL, so you cannot check if column exists using pure SQL. As a workaround you can create VBA function and use it in WHERE clause of your SQL SELECT

SQL text search on any record field

I´m building a grid system and I need to search for a text occurrence on ANY field or my record - even being strings, timestamp and numbers.
Something like:
SELECT * FROM MYTABLE WHERE ANY_FIELD CONTAINS ('MyText')
If not, what would be the way to go for a solution ?
PS: I´m using mySql as database, but I will have to support that on Oracle and SqlServer as well.
Thanks for help.
SELECT * FROM MYTABLE WHERE MATCH (ANY_FIELD_1, ANY_FIELD_2) AGAINST ('MyText' IN BOOLEAN MODE)
You can also have a look on Boolean Full-Text Searches. Here are few more operators which you might need or like to implement in the code for example
+ stands for AND
- stands for NOT
I don't know MySQL well enough to really offer any solution on that end, but since you mentioned you'll need to use this searching method on SQL Server as well, I can offer a solution for that side.
In SQL Server, you can search all columns at the same time by invoking some XML and XQuery magic:
Declare #Value nvarchar(max)
Set #Value = 'YourSearchTextHere'
;With xml As
(
Select T2.Xml, T1.SomePrimaryId
From YourTable as T1
Cross Apply
(
Select T1.* for xml path(''), type
) as T2(Xml)
)
Select c.*
From xml x
Join YourTable c on c.SomePrimaryId = x.SomePrimaryId
Where x.xml.value('contains((.),sql:variable("#Value"))','bit') = 1
Here's a working demo on SQLFiddle.
Essentially, it's converting every row in the table into XML with the columns as separate nodes, then searching that XML for any node whose value contains the value you're searching for.