oracle substr from character until character - sql

I have a string with multiple / characters. I need to trim the string before/after each of that character. Example string "231/19/2812-27/1". Each trim will be its own column.
Row1 | Row2 | Row3 | Row4 | Row5
231 | 19 | 2812 | 27 | 1
Please no regex.
Thank you.
Edit: Edited how the string should be trimmed and represented as a table (27/1)

try this
select SUBSTR('231/19/2812-27/1',1,instr('231/19/2812-27/1','/',1)-1) col1,
SUBSTR('231/19/2812-27/1',instr('231/19/2812-27/1','/',1)+1,(instr('231/19/2812-27/1','/',1,2)-instr('231/19/2812-27/1','/',1))-1) col2 ,
SUBSTR('231/19/2812-27/1',instr('231/19/2812-27/1','/',1,2)+1,(instr('231/19/2812-27/1','-',1,1)-instr('231/19/2812-27/1','/',1,2))-1) col3 ,
SUBSTR('231/19/2812-27/1',(instr('231/19/2812-27/1','-',1,1)+1)) col4
from dual;

First of all, I'd suggest representing the result in rows, not columns, because in the latter case you have to know the number of columns to construct a query or use dynamic SQL which is quite more complicated and absolutely redundant in your case.
Try this:
with t as (select '231/19/2812-27/1' as str from dual)
select regexp_substr(str,'[^/]+',1,level) as val
from t
connect by regexp_substr(str,'[^/]+',1,level) is not null
Yes, it's a regexp and, in my opinion, the easiest way to achieve the result you want. If you do not want to use this then write your own function to split a string.
But, if you still want to represent your result in columns, try this:
--edit removed a , at the end
SELECT REGEXP_SUBSTR(t.value, '[^/]+', 1, 1) col1,
REGEXP_SUBSTR(t.value, '[^/]+', 1, 2) col2,
REGEXP_SUBSTR(t.value, '[^/]+', 1, 3) col3,
REGEXP_SUBSTR(t.value, '[^/]+', 1, 4) col4
FROM (select '231/19/2812-27/1' as value from dual) t;

Related

How to extract a substring from column in oracle?

I've a column in oracle that stores values in keys. Just for example-
Column_name
((key1="value1" AND key2='value1') OR (key1="value1" AND key2='value2'))
((key1="null" AND key2='value3') OR (key1="value1" AND key2='value4'))
I want to only extract the value of key2 before OR clause (as there are 2 key2 in every row of this column)
Expected result:
Column_name
Value
((key1="value1" AND key2='value1') OR (key1="value1" AND key2='value2'))
value1
((key1="null" AND key2='value3') OR (key1="value1" AND key2='value4'))
value3
Can somebody give me roughly an idea how to do this?
Assuming we can describe your logic as extracting the first key2 value, we can try using REGEXP_SUBSTR with a capture group:
SELECT col, REGEXP_SUBSTR(col, 'key2=''(.*?)''', 1, 1, NULL, 1) AS key
FROM yourTable;

How to split string based on column length and insert into table

I have a string that I need to split and create table from it.
00001 00000009716496000000000331001700000115200000000000
I know the exact length of each column:
Col1 = 5
Col2 = 7
Col3 = 23
etc...
I need something like this (Empty values are NULL's)
Can you direct me to the right way of doing that?
Use substring():
select substring(col, 1, 5) as col1,
substring(col, 6, 2) as col2,
. . .
you can use computed column to improve your performance(visit https://www.sqlservertutorial.net/sql-server-basics/sql-server-computed-columns/)
use below function to fill your column
SUBSTRING(string, start, length)

Change comma to dot for every cell in column

In my table I have column with numbers. These numbers uses a comma as the decimal separator. I would like a dot instead for the comma for every cell in this column.
This is what I have:
Col1 Col2 NumCol
Value 1 Value 2 12,3
Value 3 Value 4 1,23
Value 5 Value 6 99,8
This is what I want:
Col1 Col2 NumCol
Value 1 Value 2 12.3
Value 3 Value 4 1.23
Value 5 Value 6 99.8
I am familiar with the REPLACE-function. However I don't know how to use it when it involves a whole column. How would a function like that look like?
Use replace()
select t.col1, t.col2, t.NumCol, replace(numcol, ',', '.') as NewNumCol
from table t;
EDIT : As side noted by #barthofland if you have large value then only one replace() might fail. So, you need instead :
replace(replace(numcol, '.', ''), ',', '.')

Alicloud SQL-Hive, SQL to exclude number only and alphabet only and single chinese word only

I have a column like below
**col1**
1244
a888d
ahahd
我
我是
19mon
The output I would like to have is
**col1**
a888d
我是
19mon
I was trying to use syntax below to exclude number only and alphabet only, however no strings were printed out. And I don't know how to exclude single chinese word, like"我" above.
SELECT col1 from abc
where col1 like '%[^0-9.]%' AND col1 like '%[^a-zA-Z.]%'
Any ideas how to solve this? Thank you!
Using regex:
with your_data as (
select stack(6,
'1244',
'a888d',
'ahahd',
'我',
'我是',
'19mon'
) as col1
)
select col1 from your_data
where col1 not rlike ('^\\d+$') --not digits only
and col1 not rlike ('^[a-zA-Z]+$') --not alpha only
and length(col1) !=1; --not single char (digit and alpha filtered already)
Returns:
col1
a888d
我是
19mon
You could try something like:
SELECT *
FROM abc
WHERE LOWER(col1) != UPPER(col1) -- COLLATE Latin1_General_CS_AS SQL Server specific
OR (LENGTH(col1) != 1 AND col1 like '%[^a-zA-Z.0-9]%');
db<>fiddle demo

Extract Value from a string PostgreSQL

Simple Question
I have the following type of results in a string field
'Number=123456'
'Number=1234567'
'Number=12345678'
How do I extract the value from the string with regard that the value can change between 5-8 figures
So far I did this but I doubt that fits my requirement
SELECT substring('Size' from 8 for ....
If I can tell it to start from the = sign till the end that would help!
The likely simplest solution is to trim 7 leading characters with right():
right(str, -7)
Demo:
SELECT str, right(str, -7)
FROM (
VALUES ('Number=123456')
, ('Number=1234567')
, ('Number=12345678')
) t(str);
str | right
-----------------+----------
Number=123456 | 123456
Number=1234567 | 1234567
Number=12345678 | 12345678
You could use REPLACE:
SELECT col, REPLACE(col, 'Number=', '')
FROM tab;
DBFiddle Demo
Based on this question:
Split comma separated column data into additional columns
You could probably do the following:
SELECT *, split_part(col, '=', 2)
FROM table;
You may use regexp_matches :
with t(str) as
(
select 'Number=123456' union all
select 'Number=1234567' union all
select 'Number=12345678' union all
select 'Number=12345678x9'
)
select t.str as "String",
regexp_matches(t.str, '=([A-Za-z0-9]+)', 'g') as "Number"
from t;
String Number
-------------- ---------
Number=123456 123456
Number=1234567 1234567
Number=12345678 12345678
Number=12345678x9 12345678x9
--> the last line shows only we look chars after equal sign even if non-digit
Rextester Demo