How do i remove string form a column in SQL? - 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%';

Related

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

Replace first few characters of field in an update statement

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)

replace data in a column after some characters in oracle

I have a column with values like:
1492966EMAIL1ABCDEFGHIJK12/22/2012 04:20:35
I want to replace the whole part after EMAIL1 in the column and this has to be done for more than 500000 rows. The problem is that the number of digits before EMAIL1 is not common in all the rows, but the value EMAIL1 is there in all the rows. I am not able to find the right function to go about this as I have tried using substr and trim, but I am not able to get the right query for this.
Can someone please tell me how this can be achieved in Oracle SQL? Let me know if more details are needed on the same.
This will exactly do your purpose,
Select SUBSTR(val,1,instr(val,'EMAIL1')+5) from table1
fiddle_demo
Get the string after 'EMAIL1' and replace it required string,
Select replace(SUBSTR(val,instr(val,'EMAIL1')+5),
'String you want to replace','string that replaces')
from table1
demo
update table1 set val=(Select replace(SUBSTR(val,instr(val,'EMAIL1')+5),
'String you want to replace','string that replaces') from table1)
where lower(val) like '%email1%';
update_demo
i hope your requirement is to concatenate a string after email1..for this sub-string position till email1 and concatenation with your required string ..
select SUBSTR(column_name,1,INSTR(column_name,'EMAIL1')+5)||'string' from table1
It seems like you can use the functions REPLACE(), SUBSTR() and, INSTR() in the following:
select
replace(yourcol, substr(yourcol, instr(yourcol, 'EMAIL1')+6), '') newCol
from yourtable
See SQL Fiddle with Demo
Your final value will be:
| NEWCOL |
-----------------
| 1492966EMAIL1 |
Then if you were to use this in a UPDATE statement, the query would be:
update yourtable
set yourcol = replace(yourcol, substr(yourcol, instr(yourcol, 'EMAIL1')+6), '');
See SQL Fiddle with Demo
Use INSTR function to find position of occurence of string EMAIL1 .
Select substring starting from beginning of original string upto
starting position of EMAIL1 plus 6 characters . 6 is length of EMAIL1 substring.
If you want to select in this manner do
SELECT substr( column_name, 1, instr(column_name,'EMAIL1')+6)
If you want to update column values
UPDATE <table>
SET column_name = substr( column_name, 1, instr(column_name,'EMAIL1')+6)