How to Replace text from postgresql data? - postgresql

My PostgreSQL data has "id" column.
This column has each item's ID
Example.
PLPL-WIT-2009-00537
DE14531/11-1
NLRTD-2014-0603
some of them has "/" in ID (ex.DE14531/11-1 contains "/")
I want to replace all "/" to "--"
So I tried this
UPDATE table_name SET id=REPLACE(id,"/","--");
but an error occurred.
ERROR: column "/" does not exist
What should I do?

Replace double quote to single quote as below:
UPDATE table_name SET id=REPLACE(id, '/', '--');

Related

How to add a single quote before each comma

a have a column as below
mystring
AC1853551,AC1854125,AC1855220,AC188115,AC1884120,AC1884390,AC1885102
I need to transformm it to get this output
mystring
('AC1853551','AC1854125','AC1855220','AC188115','AC1884120','AC1884390','AC1885102')
Here is my query that i tried
select CONCAT('( , CONCAT (mystring, ')')) from mytablename
I'm getting an error when it comes to insert a single quote '
Then i thought about replacing the comma with a ','
How to get desired output
i'm using postgres 10
A literal quote is coded as a doubled quote:
select '(''' || replace(mycolumn, ',', ''',''') || ''')'
from mytable
See live demo.

Escaping column with dot in UPDATE in PostgreSQL 9.3.9

I need to fix few lines in Jira configuration in PostgreSQL. Unfortunatelly it uses dot notation in columns and I cannot fine a way how to escape it.
select * from "cwd_directory_attribute";
directory_id | attribute_name | attribute_value
1 | user_encryption_method | atlassian-security
10000 | ldap.user.filter | (objectclass=person)
No escaping:
psql:fix_directory.sql:1: ERROR: column "ldap" of relation "cwd_directory_attribute" does not exist
LINE 1: update cwd_directory_attribute set ldap.basedn='ou=sandbox,d...
Single quote:
psql:fix_directory.sql:1: ERROR: syntax error at or near "'ldap.basedn'"
LINE 1: update cwd_directory_attribute set 'ldap.basedn'='ou=sandbox...
Double quote:
psql:fix_directory.sql:1: ERROR: column "ldap.basedn" of relation "cwd_directory_attribute" does not exist
LINE 1: update cwd_directory_attribute set "ldap.basedn" ='ou=sandb...
Square bracket:
psql:fix_directory.sql:1: ERROR: syntax error at or near "["
LINE 1: update cwd_directory_attribute set [ldap.basedn] ='ou=sandb...
E.g.
update cwd_directory_attribute set "ldap.basedn" ='ou=sandbox,dc=eu' where directory_id=10100;
I found answers that double quotes shall work but they do not. I could modify the columns manually in some SQL editor, but I want to set up a script:
psql -f fix_directory.sql jiradb7
What is the proper escaping for columns in UPDATE procedure?
Update:
select "ldap.secure" from "cwd_directory_attribute";
ERROR: syntax error at or near "select"
LINE 2: select "ldap.secure" from "cwd_directory_attribute";
You are confusing column values and column names.
Your table has a column named attribute_name which contains the value 'ldap.user.filter' (and supposedly 'ldap.basedn' in a different row).
So what you want is:
update cwd_directory_attribute
set attribute_value = 'ou=sandbox,dc=eu'
where directory_id = 10100
and attribute_name = 'ldap.basedn';

How do you index a text column in postgreSQL?

In MySQL Key Length is added as a type-modifier and placed in parenthesis colname(), one can provide it to CREATE INDEX like this,
CREATE INDEX foo_bar_idx ON foo ( bar(500) );
scenario :
Mysql custom db api function:
def add_index(self, doctype, fields, index_name=None):
"""Creates an index with given fields if not already created.
Index name will be `fieldname1_fieldname2_index`"""
index_name = index_name or self.get_index_name(fields)
table_name = 'tab' + doctype
if not self.has_index(table_name, index_name):
self.commit()
self.sql("""ALTER TABLE `%s`
ADD INDEX `%s`(%s)""" % (table_name, index_name, ", ".join(fields)))
using the above for mysql as;
hotelier.db.add_index("Item", ["route(500)"])
postgresql custom db api function:
def add_index(self, doctype, fields, index_name=None):
"""Creates an index with given fields if not already created.
Index name will be `fieldname1_fieldname2_index`"""
index_name = index_name or self.get_index_name(fields)
table_name = 'tab' + doctype
self.commit()
self.sql("""CREATE INDEX IF NOT EXISTS "{}" ON `{}`("{}")""".format(index_name, table_name, '", "'.join(fields)))
How to call the same thing in postgresql ?
hotelier.db.add_index("Item", ["route(500)"])

The column index is out of range: 2, number of columns: 1 error while updating jsonb column

I am trying to update jsonb column in java with mybatis.
Following is my mapper method
#Update("update service_user_assn set external_group = external_group || '{\"service_name\": \"#{service_name}\" }' where user=#{user} " +
" and service_name= (select service_name from services where service_name='Google') " )
public int update(#Param("service_name")String service_name,#Param("user") Integer user);
I am getting the following error while updating the jsonb (external_group) cloumn.
### Error updating database. Cause: org.postgresql.util.PSQLException: The column index is out of range: 2, number of columns: 1.
### The error may involve com.apds.mybatis.mapper.ServiceUserMapper.update-Inline
I am able to update with the same way for non-jsonb columns.
Also if I am putting hardcoded value it's working for jsonb columns.
How to solve this error while updating jsonb column?
You should not enclose #{} in single quotes because it will become part of a literal rather than a placeholder. i.e.
external_group = external_group || '{"service_name": "?"}' where ...
So, there will be only one placeholder in the PreparedStatement and you get the error.
The correct way is to concatenate the #{} in SQL.
You may also need to cast the literal to jsonb type explicitly.
#Update({
"update service_user_assn set",
"external_group = external_group",
"|| ('{\"service_name\": \"' || #{service_name} || '\" }')::jsonb",
"where user=#{user} and",
"service_name= (select service_name from services where service_name='Google')"})
The SQL being executed would look as follows.
external_group = external_group || ('{"service_name": "' || ? || '"}')::jsonb where ...

Updating values in real time Talend

I'm trying to update my data with talend.I'm using tOracleOutput with the option 'update or insert' but my problem is that when there is an update i want to sum the older and the newer value of a row because my app is executing in real time.There is any help? (sorry for my bad english)
You can use a tOracleRow component containing a SQL update statement . Take the new value (from context, row, or globalMap) and use that in the update statement within tOracleRow, adding it to the existing value of the desired record.
Ex
"
UPDATE target_table
SET target_field = target_field " + globalMap.get("newVal") + "
WHERE target_id = " + globalMap.get("id") + "
"