Update whole row in PostgreSQL - sql

I need to update a whole row without knowing the columns and rows...
Something like this:
UPDATE blabla SET blabla.* = tmp.* FROM tmp WHERE id = someid;
Has anyone ideas??
Thanks for answers

I don't think Postgres allows this. One simple solution is two separate steps:
delete from blabla where id = someid;
insert into blabla
select * from tmp where id = someid;
This assumes that the columns are defined in the same order.

I don't think its possible to update all your columns names without knowing it. Please look at this link below. Although it is in mysql. I am sure, this will apply in postgresql
SQL Updating Rows Without Knowing Column Name
I found this link which uses CTE but it specifies column names
https://dba.stackexchange.com/questions/45642/bulk-update-of-all-columns
Hope you find my answer useful.
Thanks,

Related

How to use the like function to delete the table record?

I have a problem to use the like % function to delete the table record, it cannot follow the 299-1_B to delete the row in the table.
I am used below the sample code to delete:
DELETE FROM filing_code_management WHERE folder_location LIKE '299-1_B%'
table structure
no|folder_location
-- ------------------------------------
1 299_A/299-1_B/299-1-1_C/299-1-1-1_D
2 299_A/299-1_B
3 300_B/300-1_C
3 299_A/299-1_B/299-1-1_C/299-1-1-2_F
4 299_A/299-1_B/299-1-2_P/299-1-2-1_Q
If success to show the result, the result just show 300_B/300-1_C record. Hope someone can guide me which part I am getting wrong.
Please use below query,
DELETE FROM filing_code_management WHERE folder_location LIKE '%299-1_B%'
I think you want to find the pattern anywhere in the string:
folder_location LIKE '%299-1_B%'
Or perhaps:
folder_location LIKE '299-1_B%' OR folder_location LIKE '%/299-1_B%'
That said, you appear to be storing multiple values in a string. That is bad, bad, bad. You should have a separate table with one row per folder location, rather than forcing all of them into a single string.

Add column with substring of other column in SQL (Snowflake)

I feel like this should be simple but I'm relatively unskilled in SQL and I can't seem to figure it out. I'm used to wrangling data in python (pandas) or Spark (usually pyspark) and this would be a one-liner in either of those. Specifically, I'm using Snowflake SQL, but I think this is probably relevant to a lot of flavors of SQL.
Essentially I just want to trim the first character off of a specific column. More generally, what I'm trying to do is replace a column with a substring of the same column. I would even settle for creating a new column that's a substring of an existing column. I can't figure out how to do any of these things.
On obvious solution would be to create a temporary table with something like
CREATE TEMPORARY TABLE tmp_sub AS
SELECT id_col, substr(id_col, 2, 10) AS id_col_sub FROM table1
and then join it back and write a new table
CREATE TABLE table2 AS
SELECT
b.id_col_sub as id_col,
a.some_col1, a.some_col2, ...
FROM table1 a
JOIN tmp_sub b
ON a.id_col = b.id_col
My tables have roughly a billion rows though and this feels extremely inefficient. Maybe I'm wrong? Maybe this is just the right way to do it? I guess I could replace the CREATE TABLE table2 AS... to INSERT OVERWRITE INTO table1 ... and at least that wouldn't store an extra copy of the whole thing.
Any thoughts and ideas are most welcome. I come at this humbly from the perspective of someone who is baffled by a language that so many people seem to have mastery over.
I'm not sure the exact syntax/functions in Snowflake but generally speaking there's a few different ways of achieving this.
I guess the general approach that would work universally is using the SUBSTRING function that's available in any database.
Assuming you have a table called Table1 with the following data:
+-------+-----------------------------------------+
Code | Desc
+-------+-----------------------------------------+
0001 | 1First Character Will be Removed
0002 | xCharacter to be Removed
+-------+-----------------------------------------+
The SQL code to remove the first character would be:
select SUBSTRING(Desc,2,len(desc)) from Table1
Please note that the "SUBSTRING" function may vary according to different databases. In Oracle for example the function is "SUBSTR". You just need to find the Snowflake correspondent.
Another approach that would work at least in SQLServer and MySQL would be using the "RIGHT" function
select RIGHT(Desc,len(Desc) - 1) from Table1
Based on your question I assume you actually want to update the actual data within the table. In that case you can use the same function above in an update statement.
update Table1 set Desc = SUBSTRING(Desc,2,len(desc))
You didn't try this?
UPDATE tableX
SET columnY = substr(columnY, 2, 10 ) ;
-Paul-
There is no need to specify the length, as is evidenced from the following simple test harness:
SELECT $1
,SUBSTR($1, 2)
,RIGHT($1, -2)
FROM VALUES
('abcde')
,('bcd')
,('cdef')
,('defghi')
,('e')
,('fg')
,('')
;
Both expressions here - SUBSTR(<col>, 2) and RIGHT(<col>, -2) - effectively remove the first character of the <col> column value.
As for the strategy of using UPDATE versus INSERT OVERWRITE, I do not believe that there will be any difference in performance or outcome, so I might opt for the UPDATE since it is simpler. So, in conclusion, I would use:
UPDATE tableX
SET columnY = SUBSTR(columnY, 2)
;

Oracle Update table to set specific attribute value in semicolon separated values

I have a column where I have values like:
Email_Password+oi8hu907b;New_eMail+Y;Email_Username+iugbhijhb8
Now I want to update New_eMail attribute for all rows which has Y to N without affecting anything else.
Please advise.
i hate it but...
update table
set column = replace(column,'New_eMail+Y','New_eMail+N')
where column like '%New_eMail+Y%'
you don't need the WHERE clause but if you put a functional index on the table it may be quicker with it
Since it may be the only place in the string where '+Y;' occurs the following statement may do the trick:
update <your_table>
set <your_column> = replace(<your_column>,'+Y;','+N;')
where instr(<your_column>,'+Y;')>0
This solution differs from the others provided because it does not depend on the value of the email address.
My answer is a slight improvement over the answer from user davegreen100
Since they don't allow me to post it as a comment, I add it here.
update <<tablename>>
set <<columnname>> = replace(<<columnname>>,';New_eMail+Y;',';New_eMail+N;')
where <<columnname>> like '%;New_eMail+Y;%'

How to store part of a field value in another column in SQL

So I'm trying to get a part of a value from a column and insert that part into another column - new column. BOTH columns are in the same table. So what i want should look something like this:
id newColumn oldColumn
1 12 123 some text
2 24 246 some text
....
I know how to get 12 and 24 using SUBSTR, but how do i enter the data for each row in the table. Should i be using self-join or something else?
First you have to add new col using following command:-
ALTER TABLE TAB_NAME
ADD COLUMN COL_NAME(VARCHAR(10));
After that execute this command:-
UPDAET TAB_NAME
SET COL_NAME = SUBSTRING(OLDCOLUMN, 1, 2);
I think this might help you.
No need to join, it's just a plain UPDATE:
update tablename set newColumn = substring(oldColumn from 1 for 2)
substring is ANSI SQL, some dbms have substr and other versions.
The question is why you are doing this? What do you expect to find in newColumn if someone later updates oldColumn to another value? Maybe you should have a view instead, where newColumn always has up to date values?
Please get into the habit of ALWAYS specifying the DB engine you are using... It helps us to help you - we can provide more relevant answers.
You might want to consider using a calculated column as opposed to storing the information again.
In SQL Server you could do something like this
ALTER TABLE YourTable
ADD new_column as SUBSTRING(old_column, 1, 2);
This way you don't need to insert or update this column it is always consistent with the original column. and you just use it in your select statement in the usual way.
select new_column from YourTable

WHERE statement with one to many option

I have a query
UPDATE TBL$TEMPLATE_PARAM
SET NAME='new_name'
WHERE NAME='old_name' AND FIXED_VALUE='some_value';
I want improve it with one more WHERE expression: AND BEAN_NAME='bean' but BEAN_NAME is a row from other table TBL$TEMPLATE.
Its one to many (one template - many template params). TBL$TEMPLATE_PARAM store TEMPLATE_ID and its a foreign key to ID of TEMPLATE.
What is a best way to write WHERE NAME='old_name' AND FIXED_VALUE='some_value' AND TBL$TEMPLATE.BEAN_NAME='bean'?
You could use a subquery:
UPDATE TBL$TEMPLATE_PARAM
SET NAME='new_name'
WHERE NAME='old_name' AND
FIXED_VALUE='some_value' AND
TBL$TEMPLATE_PARAM.TEMPLATE_ID IN (SELECT ID
FROM TBL$TEMPLATE
WHERE BEAN_NAME='bean')
I like Mureinik's solution, but for completeness sake, I figured I would provide a solution which uses a merge statement.
Since we know that there is a many to one relationship between the table being updated,TBL$TEMPLATE_PARAM, and the table being used to filter out records, TBL$TEMPLATE, the merge statement is well suited.
Specifically, as the Oracle documentation states (11g), "You cannot update the same row of the target table multiple times in the same MERGE statement"
Knowing the type of relationship between these tables is essential when using a merge statement.
Here is my alternate solution:
MERGE INTO tbl$template_param ttp USING tbl$template tt ON (ttp.template_id = tt.id )
WHEN MATCHED THEN
UPDATE
SET ttp.NAME ='new_name'
WHERE 1 = 1
AND tt.bean_name = 'bean'
AND ttp.NAME = 'old_name'
AND ttp.fixed_value = 'some_value';