Replace first few characters of field in an update statement - sql

Trying to solve this one out on update statement. Each field has same number of characters and format like 'abc-def-ghi'
How do I replace the 'abc-def' part with 'xxx-xxx' if the field contains 'abc'?

A combination of substrs and string concatenations should do the trick:
UPDATE my_table
SET col1 = CONCAT('xxx-xxx-' , SUBSTR(col1, 9))
WHERE col1 LIKE '%abc%'

try this:
IF(col LIKE '%abc%', REPLACE(col, 'abc-def', 'xxx-xxx'), col)

Related

How do i remove string form a column in SQL?

Here is my column :
column
abc1234
abc5678
abc4567
Now I need to remove the abc only from the column. Please help me write a query.
You might want to use REGEXP_REPLACE here:
UPDATE yourTable
SET col = REGEXP_REPLACE(col, '^abc', '')
WHERE col LIKE 'abc%';
If you don't care about the particular position of abc, and accept removing all occurrences of it anywhere, then we can do without regex:
UPDATE yourTable
SET col = OREPLACE(col, 'abc', '')
WHERE col LIKE 'abc%';

Change data remove after the parens

HI I have data in one column as this:
in SQL Server 2008
Aetna (AETNA)
I need to update this so that it will be Aetna that is to remove everything after the first parens in an update mode.
You can use CHARINDEX() to find the position of the (, then select everything to the LEFT() of that:
SELECT RTRIM(LEFT('Aetna (AETNA)',CHARINDEX('(','Aetna (AETNA)')-1))
Need to subtract 1 from the length to also remove the (, and RTRIM() removes any extra blank space from the right side.
Just replace the hardcoded string with your column name.
The update would be:
UPDATE table
SET col = RTRIM(LEFT(col,CHARINDEX('(',col)-1))
WHERE col like '%(%'
If you need to do this for all records:
update t
set col = left(col, charindex(' (', col))
where col like '% (%';

How to remove zeros from a column in db2 table

I have a column with data zeros after 6th column
i want to remove the leading zero after the 6th pipe in the data.
Please let me know if there is any way to do it. I tried to use substr with Trim but its not working.
Let's say your column is called COL something like the following should work:
CONCAT(SUBSTR(1,INSTR(COL,'|', 1,5)), LTRIM(SUBSTR(INSTR(COL,'|', 1,5)+1)),'0'))
Assuming from your current data that you need to only replace every occurrence of |00 with |, You can achieve thing using REPLACE function.
SELECT 'TMB|MLE020828585|74384911WA3S|="''07300058"|74384911|0013' AS Current_String,
replace('TMB|MLE020828585|74384911WA3S|="''07300058"|74384911|0013', '|00', '|') AS result_String
You can replace hard-coded value in above with your column name.
The above query generate result as below.
Current_String | result_String
------------------------------------------------------------------------------------------------------------------------
TMB|MLE020828585|74384911WA3S|="'07300058"|74384911|0013 | TMB|MLE020828585|74384911WA3S|="'07300058"|74384911|13
Hope this will help.
Oleg was correct in utilizing INSTR(), that is how I would do it. He was missing some arguments, though. Also, I wasn't able to get ltrim to work, so I cast it to an int instead. I tested and updated my table with your data using:
UPDATE mytable
SET mycolumn = Substr(mycolumn , 1, Instr(mycolumn , '|', 1, 5))
|| CAST(Substr(mycolumn , Instr(mycolumn , '|', 1, 5) + 1) AS INT)

SQL Strings - Filter by Hypen(x number)

I am trying to formulate a query that will allow me to find all records from a single column with 3 hyphens. An example of a record would be like XXXX-RP-XXXAS1-P.
I need to be able to sort through 1000s of records with either 2 or 3 hyphens.
You can REPLACE the hyphens in the string with an empty string and compute the difference of the length of original string and the replaced string to check for the number of hyphens.
select *
from yourtable
where len(column_name)-len(replace(column_name,'-',''))=3
and substring(column_name,9,1) not like '%[0-9]%'
If your records have 2 or 3 hyphens, then just do:
where col like '%-%-%-%'
This will get 3 or more hyphens. For exactly 3:
where col like '%-%-%-%' and col not like '%-%-%-%-%'
try this,
declare #t table(col1 varchar(50))
insert into #t values ('A-B'),('A-B-C-D-E'),('A-B-C-D')
select * from
(SELECT *
,(len(col1) - len(replace(col1, '-', ''))
/ len('-')) col2
FROM #T)t4
where col2=3

How to update a part of the string using replace function in tsql?

Hi
I have a column of nvarchar(1000) type. I need to get rid of encode characters from that column and replace them with their special characters.
For Example:
column value is : 'This text values contains this '&' this'.
I have to replace '&' with '&'.
First have to find the record which has '&' in the column (may be using like condition)
And then replace only this word with its special character
How do i do that? Pl. help
This will replace in the entire column
REPLACE(MyColumn, '&', '&')
You'll have to nest other replacements...
REPLACE(REPLACE(MyColumn, '&', '&'), '>', '>')
All together
UPDATE myTable
SET MyColumn = REPLACE(MyColumn, '&', '&')
WHERE MyColumn LIKE '%&%'
UPDATE mytable
SET mycol = REPLACE(mycol, N'&', N'&')
WHERE mycol LIKE '%&%'
EDIT
If you decide to replace multiple html entities in one go, the order of the replacements may change results.
For example:
<
becomes &< if you replace first & with & and then < with <, but the result will be < if you first try to replace < with < and then & with &.
If I have to do that kind of replacement, I usually replace & last for this reason. Sure, an edge case, and not something which happens often, but you never know...
Generic syntax:
UPDATE Table_Name
SET Column_Name = REPLACE(Column_Name, 'Text_To_Be_Replaced', 'New_Text')
WHERE Column_Name LIKE '%Text_To_Be_Replaced%'
Update TABLE
Set field = replace(field, '&', '&');