How to replace character in SQL - sql

I want to Replace a particular character on position 4 in sql Server ,
i know about replace or case when but my problem is that i just want to 4th position character replace ,
i am trying like
SELECT REPLACE(_NAME,0,1) AS exp FROM _EMPLOYEE
but it will not cheching 4th character
for example if _name contain IMR002001 then it should be IMR012001

Use stuff():
select stuff(_NAME, 4, 1, '#')
This replaces the substring starting at position 4 with length 1 with the string that is the fourth argument. The string can be longer or shorter than the string being replaced.
For your example:
select stuff(_NAME, 4, 1, '1')

Related

Oracle Substring a column for last index of character

I am trying to do substring in Oracle using last index of a character '_'.
Not able to get it rite. Please see the query which does a substring of second occurrence of "-". But it does not work if the string only has one occurrence of "-".
SELECT NVL(SUBSTR('TEMP_ABC', 0, INSTR('TEMP_ABC', '_',1,2)-1), 'TEMP_ABC')
FROM DUAL
Result - TEMP_ABC
Expected Result - TEMP
SELECT NVL(SUBSTR('TEMP_ABC_XYZ', 0, INSTR('TEMP_ABC_XYZ', '_',1,2)-1), 'TEMP_ABC_XYZ')
FROM DUAL
Result - TEMP_ABC
Expected Result - TEMP_ABC
Any clue on what I am doing wrong here?
In instr function, if you use -1 at the last parameter, it means last occurrence of the char string.
instr(string, '_', -1) = last occurrence of _
Thus:
select substr('TEMP_ABC',1,instr('TEMP_ABC','_',-1)-1)
from dual;
Result: TEMP
select substr('TEMP_ABC_XYZ',1,instr('TEMP_ABC_XYZ','_',-1)-1)
from dual;
Result: TEMP_ABC

Oracle extract alpha characters from string

I want to extract only alpha characters separately.
For example Output for "NIC132DA.1" should be "NIC" and "DA" separately without any numbers.
I tried following query:
select regexp_replace('NIC132DA.1','[^A-Za-z]') from dual;
I get following output: NICDA
Expected out put is "NIC" and "DA" separately.
Note: Number of alpha characters are not fixed in input string.
You can use REGEXP_SUBSTR:
select
regexp_substr('NIC132DA.1','[A-Za-z]+', 1, 1) first,
regexp_substr('NIC132DA.1','[A-Za-z]+', 1, 2) second
from dual;
It's better to use multilingual character class [:alpha:]:
select
regexp_substr('NIC132DA.1','[[:alpha:]]+', 1, 1) first,
regexp_substr('NIC132DA.1','[[:alpha:]]+', 1, 2) second
from dual;

split a string on multiple delimiters

I have a sql that returns the example string below:
Input PKIND:BCMOX:10048301-
output BCMOX:10048301
I need to write code the first substring the string on - then split it on : and return the 2 & 3 item (BCMOX:10048301)
If the string format is consistent and you want to extract everything after the first : until the first occurrence of -, use a combination of substr and instr.
select substr(col, instr(col,':')+1, instr(col,'-')-instr(col,':')-1)
from yourtable
where instr(col,':') > 0 and instr(col,'-') > 0 --to get the rows which have these 2 characters
The REGEXP_SUBSTR version. Return everything between the first colon and the first hyphen.
select regexp_substr('PKIND:BCMOX:10048301-', ':(.*)-', 1, 1, NULL, 1) from dual;

How to use regexp_substr() with group of delimiter characters?

I have a string something like this 'SERO02~~~NA_#ERO5'. I need to sub string it using delimiter ~~~. So can get SERO02 and NA_#ERO5 as result.
I create an regex experession like this:
select regexp_substr('SERO02~~~NA_#ERO5' ,'[^~~~]+',1,2) from dual;
It worked fine and returns : NA_#ERO5
But if I change the string to ERO02~NA_#ERO5 the result is still same.
But I expect the expression to return nothing since delimiter ~~~ is not found in that string. Can someone help me out to create correct expression?
[^~~~] matches a single character that is not one of the characters following the caret in the square brackets. Since all those characters are identical then [^~~~] is the same as [^~].
You can match it using:
SELECT REGEXP_SUBSTR(
'SERO02~~~NA_#ERO5',
'~~~(.*?)(~~~|$)',
1,
1,
NULL,
1
)
FROM DUAL;
Which will match ~~~ then store zero-or-more characters in a capture group (the round brackets () indicates a capture group) until it finds either ~~~ or the end-of-string. It will then return the first capture group.
You can do it without regular expressions, with a bit of logics:
with test(text) as ( select 'SERO02~~~NA_#ERO5' from dual)
select case
when instr(text, '~~~') != 0 then
substr(text, instr(text, '~~~') + 3)
else
null
end
from test
This will give the part of the string after '~~~', if it exists, null otherwise.
You can edit the ELSE part to get what you need when the input string does not contain '~~~'.
Even using regexp,to match the string '~~~', you need to write it exactly, without []; the [] is used to list a set of characters, so [aaaaa] is exactly the same than [a],while [abc] means 'a' OR 'b' OR 'c'.
With regexp, even if not necessary, one way could be the following:
substr(regexp_substr(text, '~~~.*'), 4)
In case you want all elements. Handles NULL elements too:
SQL> with tbl(str) as (
select 'SERO02~~~NA_#ERO5' from dual
)
select regexp_substr(str, '(.*?)(~~~|$)', 1, level, null, 1) element
from tbl
connect by level <= regexp_count(str, '~~~') + 1;
ELEMENT
-----------------
SERO02
NA_#ERO5
SQL>

How to replace nth character in sql server

I am trying to replace the nth character in SQL Server. I tried with it using replace():
SELECT REPLACE(ABC,0,1) FROM XXX
In above code all zeros will be replaced with one, but I only want to change it in a particular position and sometimes that position can change.
use stuff
The STUFF function inserts a string into another string. It deletes a specified length of characters in the first string at the start position and then inserts the second string into the first string at the start position.
select STUFF(ABC, starting_index, 1, 'X') from XXX
"Here your int position to replace" is the position no just replace with any int no and that position will be replaced
Note : (Thanks to pcnate for suggestion)
starting_index is your int position to replace.
You're looking for STUFF:
select STUFF(ABC, #n, 1, 'X') from XXX
This would replace the #nth character with an X.
Technically it seeks into the original string at column ABC starting at position #n, deletes 1 character, then inserts the string 'X' at that position.
You use STUFF for this:
SELECT STUFF(ABC, 5, 1, '1')
FROM XXX
This would replace the 5th character with a 1.
Use stuff():
select stuff(abc, 0, 1, 'a')
It is documented here.
Use Stuff.
STUFF(Column_Name,starting_index,
lenth_ofthestring_to_replace_from_starting_index, character_to_replce)
Example_
DECLARE #str varchar(100) = '123456789'
select #str
SELECT STUFF(#str,2,1, 'hello')
-- want to replece 1 charter starting from 2nd position with the string 'hello'
Check this.
SELECT STUFF(#str,2,25, 'hello'),len(#str)