Modifying existing field in json column in Postgres - json

So I am trying to update some entries in my postgres database. In particular I am trying to modify a field value using the existing value. For example, say I have the following json
{"var1": 10, "var2": 0.003, "var3": null}
and I want to update var2 to var2*100. I have updated values using an update statemnt, e.g.
UPDATE my_table SET json_column = jsonb_set(my_column, '{var2}', '0.003', true) WHERE (my_column->'var2') is null;
so I am trying to use an equivalent statement
UPDATE my_table SET my_column = jsonb_set(json_column, '{var2}', '(json_column->'var2)::double precision*100', true) WHERE id = 12;
however I am facing syntax errors. Has anyone tried something like this?

This can get really tricky but you can get there with some casts. It worked for me like this:
UPDATE my_table
SET my_column = jsonb_set(json_column, '{var2}', to_jsonb((json_column->'var2')::double precision*100), true)
WHERE id = 12;

Related

mysql update json attribute and another column in one query

I need to update a json value in a column as well as update another column in the same query.
Something like this:
UPDATE fixtures
SET jsonResults = '{}',
JSON_SET(jsonFixture, '$.time_status', '0')
WHERE intRefID = 88961323;
How can I accomplish this?
JSON_SET() returns a JSON document value, but an UPDATE statement needs a series of assignment expressions:
UPDATE fixtures
SET jsonResults = '{}',
jsonFixture = JSON_SET(jsonFixture, '$.time_status', '0')
WHERE intRefID = 88961323;
This replaces jsonFixture with the result of JSON_SET(), after setting a field within that document.
Compare with an UPDATE like this:
UPDATE mytable
SET i = i + 1
WHERE ...
It takes the value of i, adds 1, and then uses the result of that addition expression to replace i.

Append if not exists mysql json field

Is there a JSON function in mysql that will ignore trying to add the element if it already exists? For example:
update waitinglist SET
new = JSON_ARRAY_APPEND(new, '$', "orange")
where id=2;
update waitinglist SET
new = JSON_ARRAY_APPEND(new, '$', "orange")
where id=2;
Now my array looks like:
["apple", "orange", "orange", "orange", "orange"]
But I want it to work like a set, and just be:
["apple", "orange"]
Is there a way to do this?
I don't think so. You can test whether the value is already in the JSON in the WHERE clause.
update waitinglist SET
new = JSON_ARRAY_APPEND(new, '$', '"orange"'))
where id=2
AND NOT JSON_CONTAINS(new, '"orange"')
If you're updating multiple columns and need this to affect just this one column, you can use IF() to leave it unchanged if the value is already there.
update waitinglist SET
new = IF(JSON_CONTAINS(new, '"orange"'), new, JSON_ARRAY_APPEND(new, '$', '"orange"'))
where id=2

SQL error with variable assignment, concat, and right

I'm trying to generate an ID by concatenating bits from a few cells in a MySQL table. I want t0 get rid of - and : and only have digits in the ID. I get a syntax error with the following:
update scan_data
set #scanDate1 = replace(scanDate,'-','')
set #scanTime1 = replace(scanTime,'-','')
scanID = concat(right(scanContent,2),right(#scanDate1,2),right(#scanTime1,2))
What do I need to change?
Try this
update scan_data set scanID = concat(right(scanContent, 2),
right(replace(scanDate,'-',''), 2),
right(replace(scanTime,'-',''), 2)
);

MySql AES-Decrypted Field empty

I was trying to en- and decypt data with the corresponding MySQL-functions.
This is what I did:
INSERT INTO dbsec.tbl_credent (U_Password) VALUES (AES_ENCRYPT('secretText', SHA2('pwd123',512)));
the Primary-Key (id) was 6. So I used
SELECT dbsec.tbl_credent.U_Password FROM dbsec.tbl_credent WHERE dbsec.tbl_credent.id = '6';
I got something like this:
Ž4•ý/2Ÿ½üyÙ¤Ý'
So encryption seems to work so far.
When I start the following query:
SELECT AES_DECRYPT(dbsec.tbl_credent.U_Password, 'pwd123') FROM dbsec.tbl_credent WHERE dbsec.tbl_credent.id = '6';
Result is NULL
I used hashing for the password so I tried
SELECT AES_DECRYPT(dbsec.tbl_credent.U_Password, SHA2('pwd123',512)) FROM dbsec.tbl_credent WHERE dbsec.tbl_credent.id = '6';
Result is 73656372657454657874
As all this didn't work I tested this directly:
SELECT AES_DECRYPT(AES_ENCRYPT('secretText', SHA2('pwd123',512)), 'pwd123');
Again the Result was NULL and
SELECT AES_DECRYPT(AES_ENCRYPT('secretText', 'pwd123'), 'pwd123');
returned 73656372657454657874 again.
What do I have to do to get back the 'secretText' I have encrypted?
The Type of U_Password is text (latin1_swedish_ci), btw.

How to make a massive selected update

I have a list of values(deviceID) and I need to make an MySQL Query to update a column value, I can do the query in the following way
UPDATE Clients.Devices
SET assignedUserID='Jhon'
WHERE accountID='Delivery1' AND
(deviceID='1234' OR deviceID='1235' OR deviceID='1236')
That's is a simple example but some accountID have more than 500 devicesID so I'm looking for a shorter SQL statement
So, There's any way to pass the list of deviceID's to the SQL server?
Something like
UPDATE Clients.Devices
SET assignedUserID='Jhon'
WHERE accountID='Delivery1' AND
(deviceID=('1234','1235','1236))
Thanks.
UPDATE
if the datatype of deviceID is numberic, single quotes are not required.
maybe IN for multiple values not =
UPDATE Clients.Devices
SET assignedUserID = 'Jhon'
WHERE accountID = 'Delivery1' AND deviceID IN (1234,1235,1236)
or if the ID are in sequence use BETWEEN
UPDATE Clients.Devices
SET assignedUserID = 'Jhon'
WHERE accountID = 'Delivery1' AND deviceID BETWEEN 1234 AND 1236
You may try this too:
UPDATE Clients.Devices
SET assignedUserID='Jhon'
WHERE accountID='Delivery1' AND deviceID > = 1234 AND deviceID <= 1236
;
Maybe this will work, if using php:
$ids = join(',',$deviceIDs);
$sql = "UPDATE Clients.Devices
SET assignedUserID='Jhon'
WHERE accountID='Delivery1' AND deviceID IN ($ids)";