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)
Related
All,
I have 2 values as below in a table.
ID Name
1 Justs_S01_3456
2 S01_56788
The output I want is below.
ID Name NewName
1 Justs_S01_3456 S01_3456
2 S01_56788 S01_56788
I tried using CHARINDEX and other functions,but I am not getting the desired output.
Can some one please help me?
Thanks,
John
In SQL Server, you could use:
select t.*, stuff(name, 1, charindex('S01_', name) - 1, '') as newname
Here is a db<>fiddle.
You can use String functions.
The PATINDEX() function returns the position of a pattern in a string (beginning position 'S01' in the Name)
AND using SUBSTRING function you can extract the string from S01 position to the end of string:
SELECT *, SUBSTRING(Name,PATINDEX('%S01%',Name),len(Name)) As NewName from Table
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%';
Can anyone help me, I have a problem regarding on how can I get the below result of data. refer to below sample data. So the logic for this is first I want delete the letters before the number and if i get that same thing goes on , I will delete the numbers before the letter so I can get my desired result.
Table:
SALV3000640PIX32BLU
SALV3334470A9CARBONGRY
TP3000620PIXL128BLK
Desired Output:
PIX32BLU
A9CARBONGRY
PIXL128BLK
You need to use a combination of the SUBSTRING and PATINDEX Functions
SELECT
SUBSTRING(SUBSTRING(fielda,PATINDEX('%[^a-z]%',fielda),99),PATINDEX('%[^0-9]%',SUBSTRING(fielda,PATINDEX('%[^a-z]%',fielda),99)),99) AS youroutput
FROM yourtable
Input
yourtable
fielda
SALV3000640PIX32BLU
SALV3334470A9CARBONGRY
TP3000620PIXL128BLK
Output
youroutput
PIX32BLU
A9CARBONGRY
PIXL128BLK
SQL Fiddle:http://sqlfiddle.com/#!6/5722b6/29/0
To do this you can use
PATINDEX('%[0-9]%',FieldName)
which will give you the position of the first number, then trim off any letters before this using SUBSTRING or other string functions. (You need to trim away the first letters before continuing with the next step because unlike CHARINDEX there is no starting point parameter in the PATINDEX function).
Then on the remaining string use
PATINDEX('%[a-z]%',FieldName)
to find the position of the first letter in the remaining string. Now trim off the numbers in front using SUBSTRING etc.
You may find this other solution helpful
SQL to find first non-numeric character in a string
Try this it may helps you
;With cte (Data)
AS
(
SELECT 'SALV3000640PIX32BLU' UNION ALL
SELECT 'SALV3334470A9CARBONGRY' UNION ALL
SELECT 'SALV3334470A9CARBONGRY' UNION ALL
SELECT 'SALV3334470B9CARBONGRY' UNION ALL
SELECT 'SALV3334470D9CARBONGRY' UNION ALL
SELECT 'TP3000620PIXL128BLK'
)
SELECT * , CASE WHEN CHARINDEX('PIX',Data)>0 THEN SUBSTRING(Data,CHARINDEX('PIX',Data),LEN(Data))
WHEN CHARINDEX('A9C',Data)>0 THEN SUBSTRING(Data,CHARINDEX('A9C',Data),LEN(Data))
ELSE NULL END AS DesiredResult FROM cte
Result
Data DesiredResult
-------------------------------------
SALV3000640PIX32BLU PIX32BLU
SALV3334470A9CARBONGRY A9CARBONGRY
SALV3334470A9CARBONGRY A9CARBONGRY
SALV3334470B9CARBONGRY NULL
SALV3334470D9CARBONGRY NULL
TP3000620PIXL128BLK PIXL128BLK
I'm sure this is super easy, but how would I go about converting LastName,FirstName to LastName,FirstInitial?
For example changing Smith,John to Smith,J or Johnson,John to Johnson,J etc.
Thank You!
In case of LastName and FirstName columns:
select LastName,substr(FirstName,1,1)
from mytable
;
In case of a fullname saved in a single column:
select substr(fullname,1,instr(fullname || ',',',')-1) || substr(fullname,instr(fullname || ',',','),2)
from mytable
;
or
select regexp_replace (fullname,'([^,]*,?)(.).*','\1\2')
from mytable
;
Here is one way, using just "standard" instr and substr. Assuming your input is a single string in the format 'Smith,John':
select substr(fullname, 1, instr(fullname, ',')+1) from yourtable;
yourtable is the name of the table, and fullname is the name of the column.
instr(fullname, ',') finds the position of the comma within the input string (it would be 6 in 'Smith,John'); thensubstrtakes the substring that begins at the first position (the1in the function call) and ends at the position calculated byinstr`, PLUS 1 (to get the first initial as well).
I want to select names from a table where the 'name' column contains '%' anywhere in the value. For example, I want to retrieve the name 'Approval for 20 % discount for parts'.
SELECT NAME FROM TABLE WHERE NAME ... ?
You can use like with escape. The default is a backslash in some databases (but not in Oracle), so:
select name
from table
where name like '%\%%' ESCAPE '\'
This is standard, and works in most databases. The Oracle documentation is here.
Of course, you could also use instr():
where instr(name, '%') > 0
One way to do it is using replace with an empty string and checking to see if the difference in length of the original string and modified string is > 0.
select name
from table
where length(name) - length(replace(name,'%','')) > 0
Make life easy on yourselves and just use REGEXP_LIKE( )!
SQL> with tbl(name) as (
select 'ABC' from dual
union
select 'E%FS' from dual
)
select name
from tbl
where regexp_like(name, '%');
NAME
----
E%FS
SQL>
I read the documentation mentioned by Gordon. The relevent sentence is:
An underscore (_) in the pattern matches exactly one character (as opposed to one byte in a multibyte character set) in the value
Here was my test:
select c
from (
select 'a%be' c
from dual) d
where c like '_%'
The value a%be was returned.
While the suggestions of using instr() or length in the other two answers will lead to the correct answer, they will do so slowly. Filtering on function results simply take longer than filtering on fields.