Need an SQL script to update a new column with the values concatenated from 3 columns of the same table - sql

I need a prepare an SQL script to be given to my production support team to run this in production. We have created a new column in the DB2 table. This column should be filled with the data by concatenating 3 column values of the same table in the same row.
To give a history, all the reason text which are entered in the front end are inserted into the request table by unstringing into 3 columns. Since they had limited length, we created a new column with increased length and going forward all insert will go into the new column. But we need to move all the existing data in the old 3 columns to his new one. S this SQL update is just an one time exercise.
Table: tab_request
We added new column to have a increased character length and to align with other table nomenclature. Now I need the script to update the column reasontext as below

update set field1 || .... should be your dml script. use coalesce() function to convert those null values to ''.
Update table1
set reasontext =
(coalesce(reason_1, '') || ' ' || coalesce(reason_2,'') || ' ' || coalesce(reason_3,''))

update tab_request set reasontext=CONCAT(reason_1,' ',reason_2,' ',reason_3)

If you want to avoid unnecessary spaces -- in the event that some reasons are NULL -- then you can use trim() with coalesce():
update table1
set reasontext = trim(coalesce(' ' || reason_1, '') ||
coalesce(' ' || reason_2, '') ||
coalesce(' ' || reason_3, '')
);
This is equivalent to concat_ws() available in some databases.

Related

Inserting space between the string (SQL Server)

I have a table called Manufacturers which contains a column Name with a data type of Varchar(100) that has 5761 entries such as XYZ(XYZ Corp).
I wish to insert a space between the Z and the (.
I have seen the STUFF and LEFT commands but can't seem to figure out if they apply to my scenario.
You can try the REPLACE (Transact-SQL)
function as shown below.
update <yourTableName>
set <yourColumName> = replace(<yourColumName>, '(', ' ( ')
where <put the conditions here>
Here is an implementation to you.
create table test (Name varchar(20))
insert into test values ('XYZ(XYZ Corp)')
--selecting before update
select * from test
--updating the record
update test
set Name = replace(Name, '(', ' (')
where name like '%(%' --Here you can add the conditions as you want to restrict the number of rows to be updated based on the available data or the patterns.
--selecting after update
select * from test
Live Demo
I would recommend:
update Manufacturers
set name = replace(name, '(', ' (')
where name like '%[^ ](%';
This will only update rows where there is not already a space before the open paren.
Note: If you have multiple open parens, it will update all of them. If that is an issue, ask a new question with appropriate sample data.

how to avoid reaching max size of varchar2 in concat with update

I am new to oracle. when updating a table i am using
update my_table set column_name =concat(" ' ", column_name, "viewed by xxxx")
where item_value=2.
this value is being appended whenever some one is viewing...
Now my question is How to avoid max size of varchar(2) and set if it is just 50 characters lesser than its size. I am using this in servlet
My requirement is ..If some user views the item_value of 2, his userid is to be entered in my_table by concatinating the existing values in column_name where item_value=2, in one word I dont want to loose the earlier data in field when updating and to avoid reaching the max size of the field which is varchar2(900).
If the field reaches max size then the existing data will be ovewritten by the new data else it will be a concatination to existing data.
Just use SUBSTR:
update my_table set column_name
= SUBSTR(' '' ' || column_name || 'viewed by xxxx')
, 1, 900)
where item_value=2
P.S. concat can only take 2 arguments - I've converted your code to use the simpler || concatenation operator instead.
P.P.S. string literals in Oracle are delimited by ' not "
You should create a stored procedure that you will call to update that field. It will check what the new size will be after the update, and if it is too large then perform whatever action you need to (ie. add to archive table, trim off existing data, etc.).

Oracle Update Column LTRIM and RTRIM

I am wish to update my database data in SQL Developer using
UPDATE TABLE_NAME SET COLUMN_NAME = LTRIM(RTRIM(COLUMN_NAME))
But it does not take any effect even the msg of "rows updated" is displayed. The leading and trailing white spaces still exist behind every strings.
Any ideas?
Do you commit after update?
update tableName set col1 = trim(col1);
commit;
I had the same problem on two different tables. One the first table trim was ok, on the second no effect, the spaces were still there!
The difference was that in the first table I was using varchar2(30) and in the second table (where trim didn't work) I had char(30)
After modifying the second table from char(30) to varchar2(30) trim worked as expected.
Try this
UPDATE TABLE_NAME SET COLUMN_NAME = TRIM(COLUMN_NAME);
Trim Reference
Have you tried REGEXP_REPLACE(your column name, '\s*', '') ?
ex: UPDATE TABLE_NAME SET COLUMN_NAME = REGEXP_REPLACE(COLUMN_NAME, '\s*', '')
You get a message saying that n rows are updated because there are n rows in your table and you are applying the update to all of them.
To limit the update to only those rows where the update will have an effect, use:
UPDATE TABLE_NAME
SET COLUMN_NAME = LTRIM(RTRIM(COLUMN_NAME))
WHERE COLUMN_NAME != LTRIM(RTRIM(COLUMN_NAME));

Remove extra space from column values

I have a column in Oracle database, where I had stored concatenated values. For example,
/uk/letters/default?/uk/letters/funny_letters?
/uk/letters/letters?/library/conditionalstyle?o=3&f=11/uk/letters/funny_letters?
/uk/workinglife/viewarticle_93?/library/conditionalstyle?/uk/financialcentre/car_tax_calculator?
/uk/job-hunting/default?/partners/msn/i-resignfinctr?/uk/letters/letters?/
In between the urls, there are some spaces in between. How can I remove them?
UPDATE YourTable
SET YourColumn = REPLACE(YourColumn, ' ', '')
This is too late but will help some one.
select regexp_replace(column_name, '[[:space:]]+', chr(32)) from table_name;
After verification , you can update the table as follows;
UPDATE table_name
SET column_name = REGEXP_REPLACE (column_name ,'[[:space:]]+',' ');

Update column to insert spaces in one of the columns in sql server table

How do I achieve the following by a stored procedure or sql script in sql server?
Say for example, I have a-b in one of the columns and I need to update that column to a - b instead.
How do I insert spaces before and after the dash in existing data in my database.
UPDATE YourTable
SET YourColumn = REPLACE(YourColumn, '-', ' - ')
update TableName set ColumnName = Replace(ColumnName, '-', ' - ') where SomeCondition ...