How to manipulate String count in JSON in SQL Server - sql

In one use case I need to write a Query which return 1 if string count is >=4 or else return 0.
Below is the JSON which contains string.
Below is the query, which is returning a single row.
Select * from [Frs_def_businessobjectlayouts] where Definition like '%Open In Parent%' AND name like
'Task.ResponsiveAnalyst'
Note - Definition is the column which contains JSON data
Could some one help me out here!!.

First, you need to query the JSON and store in a temp variable and declare the word you want to search in another variable and perform the below operation. You will get the number of times it occurred in the JSON as output.
DECLARE #string VARCHAR(MAX)="Query your JSON from the table and assign to this variable"
DECLARE #tosearch VARCHAR(MAX)='Open In Parent'
SELECT (DATALENGTH(#string)-DATALENGTH(REPLACE(#string,#tosearch,'')))/DATALENGTH(#tosearch)
AS OccurrenceCount

Related

Getting different results from LIKE query and stored procedure for (starts and ends with) search

I am trying to implement a stored procedure that gets the two parameters #startsWith and #endsWith and constructs this query string:
#startswith + '%' + #endsWith
To search for entries of a single column (Name) that start end end with the parameters. Here is the stored procedure:
CREATE PROCEDURE termNameStartsEndsWith(
#startsWith AS nvarchar,
#endsWith AS nvarchar
)
AS
BEGIN
SELECT * FROM Term WHERE
Name LIKE (#startsWith + '%' + #endsWith)
END;
However, I get unexpected results when one of the two query parameters is empty (''). Here is an example where I would expect only results where the Term column entry starts with 'water', but i get a bunch of additional rows:
I dont get these results when executing as a query:
So I expect that the problem is coming from the empty string concatenation being handled differently in a stored procedure? If so, how can I adapt the procedure accordingly?
Thanks for the help in advance.
As noted by Larnu in the comments, the issue isn't the query, it's your parameter declarations.
You have two NVARCHAR(n) parameters declared, but there is no length declared for either of them. From the documentation (emphasis added):
When n is not specified in a data definition or variable declaration statement, the default length is 1. When n is not specified with the CAST function, the default length is 30.
So both parameters are exactly one character long. Conveniently, SQL Server will let you assign a longer value to that parameter, and then just take the first character and silently truncate the rest.
Modify your parameters to have length definitions, and you should be in business.

Prevent double-escaped JSON in FOR JSON output in SQL

I have a small problem in my case because in my case a column can contain text 'John' directly or text as array '["John","Smith"]' both. So how can I prevent double-escaped JSON in FOR JSON output? I think I am doing something wrong here. Please check my example:
Create table #jsonTest(NameList varchar(max))
insert into #jsonTest(NameList)
select '["John","Smith"]'
Now if I want its output it will give correct output from this (without escape character):
select JSON_QUERY(NameList) NameList from #jsonTest for json auto
Output:
[{"NameList":["John","Smith"]}]
Simple text example:
truncate table #jsonTest
insert into #jsonTest(NameList)
Select 'John'
Now for this I have to change my select query for the correct output because JSON_QUERY, as mentioned, it only returns objects and arrays. So i've changed it to this:
select case when ISJSON(NameList) = 1 then JSON_QUERY(NameList) else NameList end NameList from #jsonTest for json auto
Output:
[{"NameList":"John"}]
Now It will give correct output for now but if I insert previous data again and try upper select query
truncate table #jsonTest
insert into #jsonTest(NameList)
select '["John","Smith"]'
select case when ISJSON(NameList) = 1 then JSON_QUERY(NameList) else NameList end NameList from #jsonTest for json auto
Output:
[{"NameList":"[\"John\",\"Smith\"]"}]
then it is giving escape characters in output. What is wrong in the code?
This behaviour is explained in the documentation - If the source data contains special characters, the FOR JSON clause escapes them in the JSON output with '\'. Of course, as you already know, when JSON_QUERY() is used with FOR JSON AUTO, FOR JSON doesn't escape special characters in the JSON_QUERY return value.
Your problem is the fact, that your data is not always a JSON. So, one possible approach is to generate a statement with duplicate column names (NameList). By default FOR JSON AUTO does not include NULL values in the output, so the result is the expected JSON. Just note, that you must not use INCLUDE_NULL_VALUES in the statement or the final JSON will contain duplicate keys.
Table:
CREATE TABLE #jsonTest(NameList varchar(max))
insert into #jsonTest(NameList)
select '["John","Smith"]'
insert into #jsonTest(NameList)
Select 'John'
Statement:
SELECT
JSON_QUERY(CASE WHEN ISJSON(NameList) = 1 THEN JSON_QUERY(NameList) END) AS NameList,
CASE WHEN ISJSON(NameList) = 0 THEN NameList END AS NameList
FROM #jsonTest
FOR JSON AUTO
Result:
[{"NameList":["John","Smith"]},{"NameList":"John"}]

How to pass multiply values parameter to a procedure

I want to pass multiply values parameter to my procedure and use it as a filter. My parameter is called #Month and it's datatype is NVARCHAR(MAX) in the procedure.
I have used filter as
WHERE (cal.CalendarYear = #Year) AND (cal.MonthId IN (#Month))
and also tried STRING_SPLIT function.
However, when I run my report, it return an error
Conversion failed when converting the nvarchar value '1,2,3,4,5,6,7,8,9,10,11,12' to data type int.
you cannot give a varchar string with comma separated values, and expect the query to process it as a 'in' list.
The way that would work is when you would concatenate your sql statement in the stored procedure, and then execute the sql string with sp_executesql.
Using string_split is one way of doing it, but you have to be aware that it gives you a table with varchars, not integers.
For completeness I should mention that the most 'robust' way of doing this is passing a table type variable to your stored procedure, as described here: How to pass table value parameters to stored procedure from .net code.
With the split_string, you could try something like this:
select
-- your fields
from
manytables mt
join string_split(#Month,',') months on cast(months.value as int) = cal.monthId
where
cal.Calenderyear = #year

SELECT INTO INTEGER ARRAY

I want to assign values in a table column (column type is integer) to integer array. Unfortunately I could not.
--TYPE--
CREATE OR REPLACE TYPE ABILITY_ID_ARRAY IS VARRAY(100) OF INTEGER
--DECLARE IN PROCEDURE
ABILITY_IDS ABILITY_ID_ARRAY;
--STATEMENT--
SELECT ABILITY_FK INTO ABILITY_IDS
FROM T_EDUCATION_ABILITY_REL
WHERE EDUCATION_FK = edu_id;
I received this error:
[Error] ORA-00932 (16: 12): PL/SQL: ORA-00932: inconsistent datatypes: expected UDT got NUMBER
You need to use BULK COLLECT clause to store a resultset into a collection variable.
Please study the documentation of SELECT INTO statement:
https://docs.oracle.com/database/121/LNPLS/selectinto_statement.htm#LNPLS01345
into_clause
With this clause, the SELECT INTO statement retrieves one or more columns from a single row and stores them in either one or
more scalar variables or one record variable.
bulk_collect_into_clause With this clause, the SELECT INTO statement retrieves an entire result set and stores it in one or more
collection variables.
The PL/SQL statament should look like this in your case:
SELECT ABILITY_FK BULK COLLECT INTO ABILITY_IDS
FROM T_EDUCATION_ABILITY_REL
WHERE EDUCATION_FK = edu_id;

comparing input parameter with xml value using like in sql

I have an SQL table with a column which stores xml like this
<AdditionalInfo><RegistrantID>16279</RegistrantID></AdditionalInfo>
I have created a stored procedure like this:
CREATE PROC hr_GetJobStatusByRegistrantId
#registrantId VARCHAR
AS
BEGIN
SELECT TOP 1
[IsSubscribed]
FROM [Hrge].[dbo].[hr_Jobs]
where AdditionalInfo LIKE '%<AdditionalInfo><RegistrantID>%' + #registrantId + '%</RegistrantID></AdditionalInfo>%'
END
When I run this stored procedure, I get null:
exec hr_GetJobStatusByRegistrantId '16279'
If I make this parameter integer then I get convertion to int error.
Please suggest me solution to this.
(Just expanding the comment into an answer)
You should always specify the width of a char or a varchar field, because unless you do the default kicks in. The documentation says:
When n is not specified in a data definition or variable declaration
statement, the default length is 1. When n is not specified when using
the CAST and CONVERT functions, the default length is 30.
which means that in your case you have actually defined #registrantId as VARCHAR(1) so the value of '16279' was trimmed to a single character ('1') and you actually searched for
%<AdditionalInfo><RegistrantID>%1%</RegistrantID></AdditionalInfo>%
in the database. This actually returned the IsSubscribed flag for the first record it found in the DB that had a '1' anywhere in the RegistrantID field. You got lucky that the value was something wrong, so you noticed it.
Additionally you are using % around your parameter. This means that when you search for a RegistrantID of 123, you'll get results for 123, 1234, 2123, 51236, etc, etc, and then just take the first one, whichever that one is (decided by the database, since there is no order clause). It's my guess that you need an exact match, so you should remove those, and just use
'%<AdditionalInfo><RegistrantID>' + #registrantId
+ '</RegistrantID></AdditionalInfo>%'
Also, it the RegistrantId is actually a number, it would be nice if the interface of the procedure reflected that, so it could be defined with
#registrantId int
and then converted to a string in the query
'%<AdditionalInfo><RegistrantID>' + cast(#registrantId as varchar(10))
+ '</RegistrantID></AdditionalInfo>%'