Update PostgreSQL hstore field with sql variable - sql

I have table files which has hstore column details. In my sql statement I insert data to it:
UPDATE files SET details = 'new_users=>new_users_count'::hstore where id = v_file_id;
but I want to update this hstore field not with string but with variable that is available in my sql statement. How can I do this?

PL/pgSQL can't detect variables inside a string literal. You need to use the "constructor" method of the hstore type to pass a variable:
UPDATE files
SET details = hstore('new_users', p_new_user_count)
where id = v_file_id;
If p_new_user_count is defined as a number (rather than a varchar or text) you need to cast this to a text value:
UPDATE files
SET details = hstore('new_users', p_new_user_count::text)
where id = v_file_id;
Edit after the question was changed:
To do this for multiple variables you can either concatenate two hstore values:
details = hstore('new_users', p_new_user_count::text)||hstore('post_count', p_post_count::text)
or use arrays:
details = hstore(array['new_users','post_count'], array[p_user_count, p_post_count]::text[]);
This is all documented in the manual: http://www.postgresql.org/docs/current/static/hstore.html

In case if you need to replace only new_users in the hstore then
UPDATE files
SET details = details || hstore('new_users', p_new_user_count::text)
where id = v_file_id;
It will replace new_users if already present in the column or add a new entry.

Related

Add array data type to sql database

Is it possible to add array data type to the postgreSQL database?
Like this :
ALTER TABLE table_name
ADD COLUMN new_column_name ARRAY;
You will need to define the type of the array, e.g. if you want an array of integers, use int[]
ALTER TABLE table_name
ADD COLUMN new_column_name int[];
(or use the ARRAY keyword as shown in Sebastian's answer)
For more details, see the manual
You can use ARRAY on any built-in or user-defined base type, enum type, or composite type like this:
ALTER TABLE table_name ADD COLUMN new_column_name INTEGER ARRAY;
demo on dbfiddle.uk

Set a field to the value of another field [duplicate]

Is it possible to copy data from column A to column B for all records in a table in SQL?
How about this
UPDATE table SET columnB = columnA;
This will update every row.
UPDATE table_name SET
destination_column_name=orig_column_name
WHERE condition_if_necessary
This will update all the rows in that columns if safe mode is not enabled.
UPDATE table SET columnB = columnA;
If safe mode is enabled then you will need to use a where clause.
I use primary key as greater than 0 basically all will be updated
UPDATE table SET columnB = columnA where table.column>0;
If you want to copy a column to another column with a different data type in PostgresSQL, you must cast/convert to the data type first, otherwise it will return
Query 1 ERROR: ERROR: column "test_date" is of type timestamp without
time zone but expression is of type character varying LINE 1: update
table_name set test_date = date_string_col
^ HINT: You will need to rewrite or cast the expression.
An example of converting varchar to timestamp:
update table_name set timestamp_col = date_string_col::TIMESTAMP;
An example of converting varchar to int:
update table_name set int_column = string_col::INTEGER;
but any column type(except file or the similar) can be copied to string(character varying) without cast the type.

Comparing varchar2 in Oracle with different charsets?

I have a column in a table that is defined as Varchar2(4000) called tbl_varchar.
I get a input argument from the client to a stored procedure that is varchar2(4000) as well. I call that input_varchar.
I want to update only if the contents has changed:
Update table t SET tbl_varchar = input_varchar, updated = sysdate
WHERE key = input_key and tbl_varchar != input_varchar;
I am running this with the same input several times and see that my timestamp is updated everytime but the varchar data doesn't seem to be change.
Can this have anything to do with different char sets on the client and database? If so, what is the best way to confirm it? I don't have any control of the client.
If that's about charset you can try:
Update table t SET tbl_varchar = input_varchar, updated = sysdate
WHERE key = input_key and convert(tbl_varchar, 'US7ASCII') != convert(input_varchar, 'US7ASCII');
Or put your charset instead of 'US7ASCII'
I'd also add trim() both values. Maybe issue is with some whitespaces.

Updating a JSON field replaces whole document?

In sql server 2016 I am expecting a document to have 3000+ fields in a JSON column. Can I update one field in the document without replacing to whole document. How can I do this?
You could use JSON_MODIFY function:
Updates the value of a property in a JSON string and returns the
updated JSON string.
JSON_MODIFY ( expression , path , newValue )
Something like:
UPDATE table_name
SET json_column = JSON_MODIFY(json_column, '$.name', 'new_name')
WHERE id = 1;

How to update a boolean field in Oracle table

I am a novice Oracle user. I wanted to update a boolean field in my table for one of the record. Which one of these statements is correct ?
update MyTable set myBooleanColumn = 1 where UserId= 'xx12345';
or
update MyTable set myBooleanColumn = '1' where UserId= 'xx12345';
any help is greatly appreciated!! thanks !
It depends on how the field is defined.
If its defined as a CHAR(1) field, then you can store 'Y'/'N' or 'T'/'F' in it. To update the field, you'd use the quotes as it would be a string literal.
UPDATE TestTable set myCharBooleanColumn = 'Y';
If the field is defined as NUMERIC, then the convention is 0=false and 1 or -1 is true (I've seen both).
UPDATE TestTable set myNumericBooleanColumn = 1;
Many people will advocate the CHAR(1) approach, but in the real world - you see both. It depends on how the boolean is implemented.
You can read more in Oracle's docs on Datatypes
http://docs.oracle.com/cd/B28359_01/server.111/b28318/datatype.htm
There is no such thing as a Boolean field in Oracle so your field is either a numeric field or a character field. If it's a numeric field you don't need to quote the number; if it's a character field you should quote the string.
You can find out the type of the column by querying USER_TAB_COLUMNS:
select *
from user_tab_columns
where table_name = 'MYTABLE'
and column_name = 'MYBOOLEANCOLUMN'
or by describing the table.
There is nothing as Boolean field in Oracle.
The best what you can do is to create the table like this:-
create table ABC(bool char(1) check (bool in ('N','Y'));
Then simple update like
UPDATE ABC set bool = 'Y';
WHY TAKING CHAR?
There is no support for BOOLEAN, BIT, or TINYINT data types so char would be the best as it takes 1 byte