"Truncate" String Column in another column? - sql

I have a table with a column containing String values. The String values always end with a letter "T" as the last character, a space " " and a number is right after the string:
StringColumn
"asdjadhasdT 32 asjashudT 2"
"tytweytwe aweriuhfT 23"
"ajkjsdT 6 asdajkdjkjT 1445"
"kjkasd aaassT 980"
I would like to get the number in another column.
In other words:
StringColumn | ColumnValues
"asdjadhasdT 32 asjashudT 2" | 2
"tytweytwe aweriuhfT 23" | 23
"ajkjsdT 6 asdajkdjkjT 1445" | 1445
"kjkasd aaassT 980" | 980

It looks like you also have a space after the 'T'. Here is one approach:
select StringColumn, substr(StringColumn, 2 - instr(reverse(StringColumn), 'T')) as Values
from . . .
This finds the position of 'T' in the reversed string, and then takes that many characters minus two from the end of the string.
EDIT:
with t as (
select 'asdjadhasdT 32 asjashudT 2' as StringColumn from dual union all
select 'tytweytwe aweriuhfT 23' as StringColumn from dual union all
select 'ajkjsdT 6 asdajkdjkjT 1445' as StringColumn from dual union all
select 'kjkasd aaassT 980' as StringColumn from dual
)
select StringColumn,
substr(StringColumn, 2-instr(reverse(StringColumn), 'T')) as "Values"
from t;
SQL Fiddle is here.
The problem with the first version is that Values is a reserved word in Oracle, so the query fails to compile.

i took the input strings provided and
got the position of last occurence of 'T' and then took a substring below is the Query :
SELECT StringColumn,Trim(SubStr(StringColumn,INSTR(StringColumn,'T',-1 )+1)) FROM test;
Thanks,
Koustubh Avadhani

Related

Extract a number from column which contains a string

So i have a function, which returns a combination of strings (multiple values). I need to extract everything that is followed by char "DL:". But only that.
So before extraction:
**pck_import.GETdocnumber(XML_DATA)**
________________________________________
DL:2212200090001 Pr:8222046017
________________________________________
Obj:020220215541 DL:1099089729
________________________________________
DL:DST22017260
________________________________________
DL:22122000123964 Pr:8222062485
________________________________________
DL:22122000108599
________________________________________
Obj:0202200015539 DL:2100001688
In every case, i'll need the "number" after char "DL:". The "DL:" can be alone, can be at first place (between multiple values), also can be the last string. Also in some cases, the "DL:" value contains char, too.
So, output:
**OUTPUT**
______________
2212200090001
______________
1099089729
______________
DST22017260
______________
22122000123964
______________
22122000108599
______________
2100001688
I tried:
substr(pck_import.GETdocnumber(XML_DATA),
instr(pck_import.GETdocnumber(XML_DATA),
'DL:') + 3))
That returns "Pr:", too.
with s as (
select 'DL:2212200090001 Pr:8222046017' str from dual union all
select 'Obj:020220215541 DL:1099089729' str from dual union all
select 'DL:DST22017260' str from dual union all
select 'DL:22122000123964 Pr:8222062485' str from dual union all
select 'DL:22122000108599' str from dual union all
select 'Obj:0202200015539 DL:2100001688' str from dual)
select str, regexp_substr(str, 'DL:(\S+)', 1, 1, null, 1) rs
from s;
STR RS
------------------------------- -------------------------------
DL:2212200090001 Pr:8222046017 2212200090001
Obj:020220215541 DL:1099089729 1099089729
DL:DST22017260 DST22017260
DL:22122000123964 Pr:8222062485 22122000123964
DL:22122000108599 22122000108599
Obj:0202200015539 DL:2100001688 2100001688
6 rows selected
Something like this?
Sample data:
SQL> with test (col) as
2 (select
3 '________________________________________
4 DL:2212200090001 Pr:8222046017
5 ________________________________________
6 Obj:020220215541 DL:1099089729
7 ________________________________________
8 DL:DST22017260
9 ________________________________________
10 DL:22122000123964 Pr:8222062485
11 ________________________________________
12 DL:22122000108599
13 ________________________________________
14 Obj:0202200015539 DL:2100001688'
15 from dual)
16 --
Query:
17 select replace(regexp_substr(col, 'DL:\w+', 1, level), 'DL:') result
18 from test
19 connect by level <= regexp_count(col, 'DL:');
RESULT
--------------------------------------------------------------------------------
2212200090001
1099089729
DST22017260
22122000123964
22122000108599
2100001688
6 rows selected.
SQL>
(note that query might need to be modified if you'll be dealing with more than a single row of data)
You could achieve this by using regular expressions utilising a positive lookbehind and lookahead.
The regex (?<=DL\:)\d*(?=\s)' matches all digits between DL: until a single whitespace character occurs.
You'd want to use the REGEXP_SUBSTR function for this (as you tagged this question with OracleSQL):
SELECT
REGEXP_SUBSTR(my_column,
'(?<=DL\:)\d*(?=\s)') "DL field"
FROM my_table;
If you want to match substrings like DST22017260 as well, using . (any character) instead of \d would work: (?<=DL\:).*(?=\s).

Remove only first character from the field if it is 0

How to write sql query which will show values skipping first character if it is 0 (only the first character). All values are 3 characters long.
Examples:
numbers
123
023
003
102
should display as follows (after executing the query)
numbers
123
23
03
102
I used the following solution, but it removes all 0's, not just the first. How to fix it so that it only removes the first character if it is 0.
SUBSTRING(numbers, PATINDEX('%[^0]%', numbers+'.'), LEN(numbers))
I will be grateful for your help.
You can use CASE expression:
SELECT CASE WHEN LEFT(numbers, 1) = '0' THEN RIGHT(numbers, 2) ELSE numbers END AS FormattedNumbers
why not using simple substr() ?
select case when substr(mycol,1,1)='0' then substr(mycol,2) else mycol end
from my table
you did not mention your DB so i assumed its oracle. This will work in any RDBMS.
You can use charindex and substring methods to do string manipulation :)
select
case when charindex('0', number) = 1
then substring(number, 2, len(number))
else number end
from (
select '123' number
union all
select '023'
union all
select '003'
union all
select '102'
) a

ORACLE SQL - REGEXP_LIKE Contains First Character As a Number and Second Character as an Alphabet

I am trying to generate a query in Oracle where i can get records that has first character in String as 3 or 4 AND second character is an alphabet. The rest can be anything else.
Something like this
SELECT COL1 FROM TABLE
WHERE REGEXP_LIKE (COL1, '3[A-Za-Z]')
OR REGEXP_LIKE (COL1, '4[A-Za-z]')
I Do get the output but for few records the data doesn't start with 3 or 4.
Meaning it selects those records who have 3 and An alphabet together anywhere in the column.
ex: 10573T2 (10573T2). I have to query records that should start with either 3 or 4 and the next character should be a letter.
Any help would be great
SQL> with test (col) as
2 (select '10573T2' from dual union all
3 select '3A1234F' from dual union all
4 select '23XXX02' from dual union all
5 select '4GABC23' from dual union all
6 select '31234FX' from dual
7 )
8 select col
9 from test
10 where regexp_like(col, '(^3|^4)[[:alpha:]]');
COL
-------
3A1234F
4GABC23
SQL>
begins ^ with 3 or | 4
and is followed by a letter [[:alpha:]]
As of your ^ doubts: that character has two roles:
[^ ... ] - Non-Matching Character List: matches any character not in list ...
^ - Beginning of Line Anchor: match the subsequent expression only when it occurs at the beginning of a line.
You need to anchor the pattern at the beginning of the string:
REGEXP_LIKE(COL1, '^[34][A-Za-z]')
Here is a db<>fiddle

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)

retrieve a specific data from a table after a symbol in oracle

Table DATA
----------------------------
Name
ABC:000
DEF:0
ABD:000
FFF:00
GGG:000
I need only those names which contains only 3 characters post the semicolon.
In the event that the field is stored as a char() and varying, then use trim():
where trim(name) like '%:___'
with
table_name ( name ) as (
select 'ABC:000' from dual union all
select 'DEF:0' from dual union all
select 'ABD:000' from dual union all
select 'FFF:00' from dual union all
select 'GGG:000' from dual
)
-- End of SIMULATED inputs (not part of the SQL query).
-- Solution begins BELOW THIS LINE. Use your actual table and column names.
select name
from table_name
where name like '%:___'
;
NAME
-------
ABC:000
ABD:000
GGG:000
Explanation: like is a comparison operator for strings. % stands for any sequence of characters, of any length (including of length zero). : stands for itself. Underscore stands for exactly one character - ANY character. The comparison string is one % sign, one : semicolon, and three underscores.