Remove zero after first two characters - sql

I have value like the below format,
150000005705
160000004559
I would like to remove three zeros after first two digits
I tried with this SQL statement:
SELECT
'150000005705',
REPLACE('150000005705', SUBSTR('150000005705', 3, 3)) new_num
FROM DUAL
Output should be like
150005705
160004559

Use SUBSTR twice and concatenate the strings
SELECT '150000005705',
CONCAT(SUBSTR ('150000005705', 1, 2), SUBSTR ('150000005705', 6)) new_num
FROM DUAL
an alternative is to use replace if there are always 6 zero's
SELECT '150000005705',
REPLACE ('150000005705', '000000', '000') new_num
FROM DUAL

Related

How to substring last two tokens from a string separated by dot in ORACLE SQL if only 4 and 5th tokens exists

I have a bunch of strings in a table called code_table and column named code as shown where teach token in the string is separated by a dot. The number of tokens in each string can vary but I have to consider ONLY the strings that have both 4th and 5th tokens and others can be ignored. A code string can have maximum of 5 tokens. If and only if the string contain both 4th and 5th tokens, then I have to concatenate them and create a single code. Here is an example:Please Click Here
You can use INSTR to look for the 4th occurrence of a . to determine which rows should be returned, then a simple SUBSTR and REPLACE to format the result as you would like.
WITH
code_table (code)
AS
(SELECT 'ppge.34.aaa.fesl' FROM DUAL
UNION ALL
SELECT 'aler.56.fghh.sdef.wath' FROM DUAL
UNION ALL
SELECT 'apef.23.ared' FROM DUAL
UNION ALL
SELECT 'eard.wed' FROM DUAL
UNION ALL
SELECT 'wera.fgeg.23d.adef.erff' FROM DUAL)
SELECT REPLACE (SUBSTR (code,
INSTR (code,
'.',
1,
3)
+ 1),
'.')
FROM code_table
WHERE INSTR (code,
'.',
1,
4) > 0;

Remove 2 characters in oracle sql

I have a column that contains 12 digits but user wants only to generate a 10 digits.
I tried the trim, ltrim function but nothing work. Below are the queries I tried.
ltrim('10', 'column_name')
ltrim('10', column_name)
ltrim(10, column_name)
For example I have a column that contains a 12 digit number
100000000123
100000000456
100000000789
and the expected result I want is
0000000123
0000000456
0000000789
To extract the last 10 characters of an input string, regardless of how long the string is (so this will work if some inputs have 10 characters, some 12, and some 15 characters), you could use negative starting position in substr:
substr(column_name, -10)
For example:
with
my_table(column_name) as (
select '0123401234' from dual union all
select '0001112223334' from dual union all
select '12345' from dual union all
select '012345012345' from dual
)
select column_name, substr(column_name, -10) as substr
from my_table;
COLUMN_NAME SUBSTR
------------- ----------
0123401234 0123401234
0001112223334 1112223334
12345
012345012345 2345012345
Note in particular the third example. The input has only 5 digits, so obviously you can't get a 10 digit number from it. The result is NULL (undefined).
Note also that if you use something like substr(column_name, 3) you will get just '345' in that case; most likely not the desired result.
try to use SUBSTR(column_name, 2)

Removal of first characters in a string oracle sql

It may be very simple question, but I have run out of ideas.
I would like to remove first 5 characters from string.
Example string will look like:
1Y40K100R
I would like to display only digits that are after '%K' which in this case should give me result of 100R.
Please note that number after 'K' can have different amount of digits. It can be 4 digit number or 2 digit number.
Just use substr():
select substr(col, 6)
This returns all characters starting at the sixth.
There are multiple ways to return all characters after the k. If you know the string has a k, then use instr():
select substr(col, instr(col, 'K') + 1)
You can use regexp_substr
select regexp_substr('1Y40K100R', '(K)(.*)', 1, 1, 'i', 2) from dual
A way without regexp:
select substr('1Y40K100R', instr('1Y40K100R', 'K') +1) from dual
This may appear not so elegant, but it usually performs better than the regexp way.

How to extract value between 2 slashes

I have a string like "1490/2334/5166400411000434" from which I need to derive value after second slash. I tried below logic
select REGEXP_SUBSTR('1490/2334/5166400411000434','[^/]+',1,3) from dual;
it is working fine. But when i dont have value between first and second slash it is returining blank.
For example my string is "1490//5166400411000434" and am trying
select REGEXP_SUBSTR('1490//5166400411000434','[^/]+',1,3) from dual;
it is returning blank. Please suggest me what i am missing.
If I understand well, you may need
regexp_substr(t, '(([^/]*/){2})([^/]*)', 1, 1, 'i', 3)
This handles the first 2 parts like 'xxx/' and then checks for a sequence of non / characters; the parameter 3 is used to get the 3rd matching subexpression, which is what you want.
For example:
with test(t) as (
select '1490/2334/5166400411000434' from dual union all
select '1490//5166400411000434' from dual union all
select '1490//5166400411000434/ramesh/3344' from dual
)
select t, regexp_substr(t, '(([^/]*/){2})([^/]*)', 1, 1, 'i', 3) as substr
from test
gives:
T SUBSTR
---------------------------------- ----------------------------------
1490/2334/5166400411000434 5166400411000434
1490//5166400411000434 5166400411000434
1490//5166400411000434/ramesh/3344 5166400411000434
You can REVERSE() your string and take the value before the first slash. And then reverse again to obtain the desired output.
select reverse(regexp_substr(reverse('1490//5166400411000434'), '[^/]+', 1, 1)) from dual;
It can also be done with basic substring and instr function:
select reverse(SUBSTR(reverse('1490//5166400411000434'), 0, INSTR(reverse('1490//5166400411000434'), '/')-1)) from dual;
Use other options in REGEXP_SUBSTR to match a pattren
select REGEXP_SUBSTR('1490//5166400411000434','(/\d*)/(\d+)',1,1,'x',2) from dual
Basically it is finding the pattren of two / including digits starting from 1 with 1 appearance and ignoring whitespaces ('x') then outputting 2nd subexpression that is in second expression within ()
... pattern,1,1,'x',subexp2)

How to take last four characters from a varchar?

I'm trying to take the last four characters only from a varchar field. All the rows are different lengths. What function should I be using to accomplish this?
Right should do:
select RIGHT('abcdeffff',4)
SUBSTR(column, LENGTH(column) - 3, 4)
LENGTH returns length of string and SUBSTR returns 4 characters from "the position length - 4"
RIGHT ( character_expression , integer_expression )
SELECT RIGHT(column, 4) FROM ...
Also a list of other string functions.
Use the RIGHT() function: http://msdn.microsoft.com/en-us/library/ms177532(v=sql.105).aspx
SELECT RIGHT( '1234567890', 4 ); -- returns '7890'
For Oracle SQL, SUBSTR(column_name, -# of characters requested) will extract last three characters for a given query. e.g.
SELECT SUBSTR(description,-3) FROM student.course;
You can select last characters with -
WHERE SUBSTR('Hello world', -4)
I have Used RIGHT function in SQL Server and it's working.
SELECT RIGHT( your_column_name, 4 );
--It will show last 4 digit/char
You can just use
SELECT RIGHT(<Column_name>, 4) FROM <table_name>
This will be giving you all the lines but only with their last 4 characters.
tested solution on hackerrank....
select distinct(city) from station
where substr(lower(city), length(city), 1) in ('a', 'e', 'i', 'o', 'u') and substr(lower(city), 1, 1) in ('a', 'e', 'i', 'o', 'u');