I want to extract the string after the character '/' in a PostgreSQL SELECT query.
The field name is source_path, table name is movies_history.
Data Examples:
Values for source_path:
184738/file1.mov
194839/file2.mov
183940/file3.mxf
118942/file4.mp4
And so forth. All the values for source_path are in this format
random_number/filename.xxx
I need to get 'file.xxx' string only.
If your case is that simple (exactly one / in the string) use split_part():
SELECT split_part(source_path, '/', 2) ...
If there can be multiple /, and you want the string after the last one, a simple and fast solution would be to process the string backwards with reverse(), take the first part, and reverse() again:
SELECT reverse(split_part(reverse(source_path), '/', 1)) ...
Or you could use the more versatile (and more expensive) substring() with a regular expression:
SELECT substring(source_path, '[^/]*$') ...
Explanation:
[...] .. encloses a list of characters to form a character class.
[^...] .. if the list starts with ^ it's the inversion (all characters not in the list).
* .. quantifier for 0-n times.
$ .. anchor to end of string.
db<>fiddle here
Old sqlfiddle
You need use substring function
SQL FIDDLE
SELECT substring('1245487/filename.mov' from '%/#"%#"%' for '#');
Explanation:
%/
This mean % some text and then a /
#"%#"
each # is the place holder defined in the last part for '#' and need and aditional "
So you have <placeholder> % <placeholder> and function will return what is found inside both placeholder. In this case is % or the rest of the string after /
FINAL QUERY:
SELECT substring(source_path from '%/#"%#"%' for '#');
FROM movies_history
you can use the split_part string function,
syntax: split_part(string,delimiter,position)
string example: exx = "2022-06-12"
Note: can be "#ertl/eitd/record_4" etc
delimiter: any character for the above example ("-" or "/")
Position: nth position,
How it works: the above exx string will be split in x times based on the delimiter
e.g position 1- 2022, position 2-06, position 3-12
so the nth position helps choose what you want to return
thus based on your example:
syntax: slipt_part(random_number/filename.xxx,"/",2)
output: filename.xxx
Related
I am trying to remove template text like &#x; or &#xx; or &#xxx; from long string
Note: x / xx / xxx - is number, The length of the number is unknown, The cell type is CLOB
for example:
SELECT 'H'ello wor±ld' FROM dual
A desirable result:
Hello world
I know that regexp_replace should be used, But how do you use this function to remove this text?
You can use
SELECT REGEXP_REPLACE(col,'&&#\d+;')
FROM t
where
& is put twice to provide escaping for the substitution character
\d represents digits and the following + provides the multiple occurrences of them
ending the pattern with ;
or just use a single ampersand ('&#\d+;') for the pattern as in the case of Demo , since an ampersand has a special meaning for Oracle, a usage is a bit problematic.
In case you wanted to remove the entities because you don't know how to replace them by their character values, here is a solution:
UTL_I18N.UNESCAPE_REFERENCE( xmlquery( 'the_double_quoted_original_string' RETURNING content).getStringVal() )
In other words, the original 'H'ello wor±ld' should be passed to XMLQUERY as '"H'ello wor±ld"'.
And the result will be 'H'ello wo±ld'
I have a long numbered list imported into a table, the strings are in the following format:
1. fdhsglahs sdhkgs
2. urgbvdgh ndovh
3. 8yhbnxjghr nvdfo dfhioj
...
9999. vnur neeu nu
I want to remove the numbers in the beginning of the string, the "." adjacent to the number, and any number of spaces that come immediately after the "." and before the next character (that is, before the beginning of the string itself).
Can't find a method to do that in SQLite.
Please notice, some of the strings contain numbers as part of the string, which are not to be removed.
For this requirement you can use string functions like substr(), instr() and ltrim():
select ltrim(substr(col, instr(col, '.') + 1))
from tablename
Replace col with the column's name.
this code returns the part of the string after the . left trimmed of spaces.
See the demo.
If you want to update the table:
update tablename
set col = ltrim(substr(col, instr(col, '.') + 1));
See the demo.
Input string: ["1189-13627273","89-13706681","118-13708388"]
Expected Output: ["14013627273","14013706681","14013708388"]
What I am trying to achieve is to replace any numbers till the '-' for each item with hard coded text like '140'
SELECT replace(value_to_replace, '-', '140')
FROM (
VALUES ('1189-13627273-77'), ('89-13706681'), ('118-13708388')
) t(value_to_replace);
check this
I found the right way to achieve that using the below regular expression.
SELECT REGEXP_REPLACE (string_to_change, '\\"[0-9]+\\-', '140')
You don't need a regexp for this, it's as easy as concatenation of 140 and the substring from - (or the second part when you split by -)
select '140'||substring('89-13706681' from position('-' in '89-13706681')+1 for 1000)
select '140'||split_part('89-13706681','-',2)
also, it's important to consider if you might have instances that don't contain - and what would be the output in this case
Use regexp_replace(text,text,text) function to do so giving the pattern to match and replacement string.
First argument is the value to be replaced, second is the POSIX regular expression and third is a replacement text.
Example
SELECT regexp_replace('1189-13627273', '.*-', '140');
Output: 14013627273
Sample data set query
SELECT regexp_replace(value_to_replace, '.*-', '140')
FROM (
VALUES ('1189-13627273'), ('89-13706681'), ('118-13708388')
) t(value_to_replace);
Caution! Pattern .*- will replace every character until it finds last occurence of - with text 140.
I have a string 'ABC.1.2.3'
I wish to replace the middle number with 1.
Input 'ABC.1.2.3'
Output 'ABC.1.1.3'
Input 'XYZ.2.2.1'
Output 'XYZ.2.1.1'
The is, replace the number after second occurrence of '.' with 1.
I know my pattern is wrong, the sql that I have at the moment is :
select REGEXP_REPLACE ('ABC.1.2.8', '(\.)', '.1.') from dual;
You can use capturing groups to refer to surrounding numbers in replacement string later:
select REGEXP_REPLACE ('ABC.1.2.8', '([0-9])\.[0-9]+\.([0-9])', '\1.1.\2') from dual;
You could use
^([^.]*\.[^.]*\.)\d+(.*)
See a demo on regex101.com.
This is:
^ # start of the string
([^.]*\.[^.]*\.) # capture anything including the second dot
\d+ # 1+ digits
(.*) # the rest of the string up to the end
This is replaced by
$11$2
How can I select abcdef.txt from the following string?
abcdef.123.txt
I only know how to select abcdef by doing select substr('abcdef.123.txt',1,6) from dual;
You can using || for concat and substr -3 for right part
select substr('abcdef.123.txt',1,6) || '.' ||substr('abcdef.123.txt',-3) from dual;
or avoiding a concat (like suggested by Luc M)
select substr('abcdef.123.txt',1,7) || substr('abcdef.123.txt',-3) from dual;
A general solution, assuming the input string has exactly two periods . and you want to extract the first and third tokens, separated by one . The length of the "tokens" in the input string can be arbitrary (including zero!) and they can contain any characters other than .
select regexp_replace('abcde.123.xyz', '([^.]*).([^.]*).([^.]*)', '\1.\3') as result
from dual;
RESULT
---------
abcde.xyz
Explanation:
[ ] means match any of the characters between brackets.
^
means do NOT match the characters in the brackets - so...
[^.]
means match any character OTHER THAN .
* means match zero or
more occurrences, as many as possible ("greedy" match)
( ... ) is called a subexpression... see below
'\1.\3 means replace the original string
with the first subexpression, followed by ., followed by the THIRD
subexpression.
Replace the substring of anything surrounded by dots (inclusive) with a single dot. No dependence on lengths of components of the string:
SQL> select regexp_replace('abcdef.123.txt', '\..*\.', '.') fixed
from dual;
FIXED
----------
abcdef.txt
SQL>