How to Split String {} with Multiple Values - sql

Let's say that I have a type STRING column 'debugdata'. An example value for a given user looks like this:
{"TITLE_DESCRIPTION":"approve","CATEGORY":"approve"}
However, let's say there can be multiple values for the TITLE_DESCRIPTION
{"TITLE_DESCRIPTION":"No, name does not match,No, summary is not clear","CATEGORY":"Yes"}
How can I split out the "No, name does not match" and "No, summary is not clear" into two columns?
I've tried using JSON_EXTRACT and JSON_ARRAY_GET and other JSON syntax, but I can't quite break this up into two columns. Thanks!

lets say x is your map from your example
let x = {"TITLE_DESCRIPTION":"No, name does not match,No, summary is not clear","CATEGORY":"Yes"}
so all you need to do is this:
let b = (x.TITLE_DESCRIPTION).split(',')
edit: in you example you split the sentences with ',' but have ',' in the string himself so use char escape for ',' or use other char for split the sentences and send it to the split function instead of ','.

What about first use json_extract and then use the String function split?

Related

using initcap function on a json with an list of strings postgresql

I am trying to use the initcap to capitalize the first letter of the string in a list of strings of json type
The table looks like this.
I tried this first
update smfood set category = to_json(string_to_array(INITCAP(category::text),','))
where smfood.category is not null
but the results get me these slashes
any idea on how i can use initcap to capitalize the first letter in a json list of strings?
Do it like this
update smfood set category = INITCAP(category::text)::json
where smfood.category is not null;

How to use a backward search using Oracle sql

I would like to make a search for several parameters. In input string i can have fields: type and number.
Field number is a number like(3,2.15, 11.64).
Type field is a string like "com", "org", "cu", "mi"
Possible variations of input string:
3 mi
2 com
3,13--org
i would like to find some strings matching this string.
So, i have to make some
select number, type from orders
where (%number%) like '49-М' or (%type%) like '49-М'
But it doesn't work. How can i compare exactly fields?
And also i can't split string into 2 numbers, cause i don't know a separator
Your question is a little vague, but regexp_substr() should be able to help:
where (%number%) like regexp_substr('49-M', '^[0-9.]+', 1) and
(%type%) like regexp_substr('49-M', '[a-zA-Z]+$', 1)

Find substring in string

Is it possible to check if a specific substring which is in SQL Server column, is contained in a user provided string?
Example :
SELECT * FROM Table WHERE 'random words to check, which are in a string' CONTAINS Column
From my understanding, CONTAINS can't do such kind of search.
EDIT :
I have a fully indexed text and would like to search (by the fastest method) if a string provided by me contains words that are present in a column.
You can use LIKE:
SELECT * FROM YourTable t
WHERE 'random words ....' LIKE '%' + t.column + '%'
Or
SELECT * FROM YourTable t
WHERE t.column LIKE '%random words ....%'
Depends what did you mean, first one select the records that the column has a part of the provided string. The second one is the opposite.
Just use the LIKE syntax together with % around the string you are looking for:
SELECT
*
FROM
table
WHERE
Column LIKE '%some random string%'
This will return all rows in the table table in which the column Column contains the text "some random string".
1) If you want to get data starting with some letter you can use % this operator like this in your where clause
WHERE
Column LIKE "%some random string"
2) If you want to get data contains any letter you can use
WHERE
Column LIKE "%some random string%"
3)if you want to get data ending with some letter you can use
WHERE
Column LIKE "some random string%"

Search string with regular expression SQL Server

The regex I want to use is: ^(?=.*[,])(,?)ABC(,?)$
What I want to get out is:
^ // start
(?=.*[,]) // contains at least one comma (,)
(,?)ABC(,?) // The comma is either in the beginning or in the end of the string "ABC"
$ // end
Of course ABC is ought to be a variable based on my search term.
So if ABC = 'abc' then ",abc", "abc,", ",abc," will match but not "abc" or "abcd"
Better way to do this is also welcome.
The value in the record looks like "abc,def,ghi,ab,cde..." and I need to find out if it contains my element (i.e. 'abc'). I cannot change the data structure. We can assume that in no case the record will contain only one sub-value, so it is correct to assume that there always is a comma in the value.
If you want to know if a comma delimited string contains abc, then I think like is the easiest method in any database:
where ',' + col + ',' like '%,abc,%'

Can anyone help me write a sql query

jkdfhdjfhjh&name=ijkjkjkjkjkjk&id=kdjkjkjkjkjkjjjd&class=kdfjjfjdhfjhf
The above string has some characters starting with & and ending with =
for example we have &name= and I just need this from the above string.
similarly I need &id=, &class=
I need the output under a single column.
Final Extract
----------------------
&id=, &class=, &name=
can anyone help me out in writing a query for this.
You could try this :
select regexp_replace('jkdfhdjfhjh&name=ijkjkjkjkjkjk&id=kdjkjkjkjkjkjjjd&class=kdfjjfjdhfjhf', '\\w*?(&.*?=)\\w+((?=&)|$)', '\\1, ', 'g');
result:
regexp_replace
-------------------------
&name=, &id=, &class=,
Then it's up to you to remove the last ,.
The regexp_replace function is available in version 8.1 and after.
If you want the values along with each variable, I would implement this by splitting on "&" into an array and then taking a slice of the desired elements:
SELECT (string_to_array('jkdfhdjfhjh&name=ijkjkjkjkjkjk&id=kdjkjkjkjkjkjjjd&class=kdfjjfjdhfjhf','&'))[2:4];
Output in PostgreSQL 8.4 (array type):
{name=ijkjkjkjkjkjk,id=kdjkjkjkjkjkjjjd,class=kdfjjfjdhfjhf}
The example string is very wide so here's the general form to show the array slicing more clearly:
SELECT ((string_to_array(input_field,'&'))[2:4];
NOTE: You must have the extra parentheses around the string_to_array() call in order for the array slicing to work--you'll get an error otherwise.