Need help to get jsob_set working under PostgreSQL procedure,
Fetching records from child table and updating parent table's json fields value.
Assigning selected value to the variable and adding it to jsonb_set under value,
e.g
jsonb_set(jsonFieldOfDb,'{json_Column}',jsonVariable,true);
Where jsonVariable is my selected value from second table.
For more detail please look at screenshot attached.
You might need to cast the path & jsonVariable to text[] and jsonb
jsonb_set(jsonFieldOfDb,'{json_Column}'::text[],jsonVariable::jsonb,true)
if your jsonVariable is just a plain string, you might need to quote them as well
jsonb_set(jsonFieldOfDb,'{json_Column}'::text[],('"' || jsonVariable || '"')::jsonb, true)
Adding to Yurui's answer, if you want to set a dynamic value other than string, you can convert the dynamic variable to jsonb by using to_jsonb function like this:
UPDATE event AS e
SET source_json = jsonb_set(source_json, '{consumerId}'::text[], to_jsonb(p.promoter_id))
FROM promoter AS p
WHERE e.id = p.event_id and p.app_id in ('123');
Related
I would like to use the value selected in a filter as a returned column.
For instance like
SELECT :MyFilter;
but I get the following error:
No valid expression found for :Subscription. Expecting "<expression> = :MyFilter" (line 1)
You may need to declare a local variable, assign it the value from :MyFilter, and then use in your query. See the following reference: https://docs.snowflake.com/en/developer-guide/snowflake-scripting/variables.html#working-with-variables
replacing XML tag value within a large XML text value MSSQL.
Within MSSQL I have a column called form which is a text column with an extremely large XML. I need to find a certain tag and change the value of that sub tag within the tag from False to True.
This is what I currently have:
USE trainset;
UPDATE dbo.users
SET formxml = REPLACE(CAST(formxml as nvarchar(max)), '%<ttaycheckbox><name>cbTermsConditions</name><cargo>F</cargo></ttaycheckbox>%', '<ttaycheckbox><name>cbTermsConditions</name><cargo>T</cargo></ttaycheckbox>')
WHERE usersid = '0000GARX'
and formname ='ffOrderRpt'
and formxml LIKE ('%<ttaycheckbox><name>cbTermsConditions</name><cargo>F</cargo></ttaycheckbox>%')
It seems like it is doing the update;
However, after this when I do a select on this particular value the value of is still False rather than True.
What am I missing in there that is not causing it to update properly?
replace() doesn't support wildcards. So your where ... like finds the relevant records, but replace finds NOTHING, because it's looking for a literal %.
You can use XML modify and exist:
UPDATE users
SET formxml.modify('replace value of (/ttaycheckbox/cargo/text())[1] with "T"')
WHERE usersid = '0000GARX'
and formname ='ffOrderRpt'
and formxml.exist('/ttaycheckbox/name[text()[1] eq "cbTermsConditions"]') = 1
and formxml.exist('/ttaycheckbox/cargo[text()[1] eq "F"]') = 1
I have read all the related entries (here and at other sites) and have not found my situation.
I have a table (MyTable) with several date fields (MyDateField1, MyDateField2, etc) plus other fields not pertinent to this matter. All the date fields allow null values.
My application's tableadapter's insert method invokes the following stored procedure:
INSERT INTO MyTable VALUES (#MyDateField1Value, #MyDateField2Value,..., <other fields>);
using this VB code (dv is a dataview):
NewRow = dv.AddNew
NewRow(MyDate1Field) = DateValue1 <some date value taken off a window>
NewRow(MyDate2Field) = DateValue2 <some date value taken off a window>
....
NewRow.EndEdit()
MyTableTableAdapter.Insert(DateValue1, DateValue2, <etc>)
This works fine when none of the date fields is null. However, if I set:
NewRow(MyDate1Field) = DBNull.Value
I get the error message that DBNull.Value cannot be converted to a date value. How can I get a null value into MyDateField1, MyDateField2, etc.
The answer is to pass Nothing. Very simple - too bad it was so hard to find.
I know how to update a record and replace a sub string in another string:
update(conn,'tableName',{'columnName'},{'value'},'where columName=xx') %update record in database
modifiedStr = strrep(origStr, oldSubstr, newSubstr) %replaces substring with new string in another string.
Now i want to mix these two and change substring of a record in the database. How can we do that? I want a query to do this. Can we mix the two by any chance?
You don't need the {} brackets if you deal with only one column.
If you have a the column name and hold it in a variable columnName, you can try something similar:
columnName = 'product_id';
whereClause = ['where ',columName,'=',origStr];
modifiedStr = strrep(origStr, oldSubstr, newSubstr);
update(conn,'tableName',columnName,modifiedStr,whereClause);
Of course you don't need to use the variables you can just replace the desired string inside the update function, but I did it for clarification.
I want to parse a field with the following type of value:
"DAVE EBERT CONSTRUCTION~139 LENNOX STREET~SANTA CRUZ, CA 95060~~Business Phone Number:(831) 818-3170"
I would like to do a query like:
Update mytable set street = string_to_array(myfield,'~')[2]
But string_to_array does not "return" an array so it can't be chained in this way. However, it does return an array that can be used by other functions that take arrays like array_upper() so I don't know why it would not work.
My workaround is to create an array field and do this:
Update mytable set myfield_array = string_to_array(myfield,'~')
Update mytable set street = myfield_array[2]
Is there a more direct way to do this? But again, if I am extracting a lot of different array elements, maybe the less direct way performs better because you are converting string to array only once?
Try...
Update mytable set street = (string_to_array(myfield,'~'))[2]
You just need those parenthesis.
Use some extra ():
Update mytable set street = (string_to_array(myfield,'~'))[2]