I need to copy rows, but I also need, to change path field:
old path value = '<src_dir>'/workspace_id/project_id/file_id.file_format
new path value = '<src_dir>'/workspace_id/new_project_id/new_file_id.file_format
I tried to find the dot position and count from it two uuid lengths + slash and put there new slash-separated project and file IDs
overlay(path, placing '{<new_project_id>}/{<new_file_id>}' from (position('.' in path)-(36 * 2 + 1) for (36 * 2 + 1)))
But if a src_dir contains a dot in its name, the position of that dot will be taken. Is there any way to take the position of the last dot?
You can use REVERSE() function along with LENGTH() such as
SELECT LENGTH(path) - POSITION( '.' IN REVERSE(path)) + 1
FROM t
Demo
this case the last dot would be positioned as the first
Okay, I am not pro in regexp, it can it could be much prettier, but it works:
regexp_replace(path, '([0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12})\/(([0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12})\.)', '{new_project_id}/{new_file_id}.')
Related
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.
I'm querying a dataset using Oracle SQL Developer and want to create a column with partial return data from a (CLOB) in another column.
The part I need is in quotes and i've used substring to extract however the value will change as will the length of the string. Is there a way to end when reach closing quotes instead of specifying length of string?
dbms_lob.substr(a.LINETESTRESULT,15,dbms_lob.instr(UPPER(a.LINETESTRESULT),'LHCRAG')+11) AS REFRESH_RAG
At the minute, 15 characters are returned, but the latest additions are shorter and showing the ", from the next entry. I need to replace the length attribute.
The result I get is:
Red_Session",
I need the output to be: Red_Session
or whatever the return value is, I just need it to end before the closing quote.
INSTR has a parameter nth (Occurrence number, starting at 1.). With this you can create something like this.
dbms_lob.substr(a.LINETESTRESULT,dbms_lob.instr(a.LINETESTRESULT, '"', 1, 2) - dbms_lob.instr(a.LINETESTRESULT, '"') - 1, dbms_lob.instr(a.LINETESTRESULT, '"') + 1) AS REFRESH_RAG
you might try REGEXP_SUBSTR like below. Alternatively you can find the position of the quote and use the substr...
SELECT
REGEXP_SUBSTR(a.LINETESTRESULT,
'"([^"]*)') AS REFRESH_RAG
FROM DUAL;
Managed to get this using the following code:
SELECT
dbms_lob.substr(UPPER(a.LINETESTRESULT), dbms_lob.instr(UPPER(a.LINETESTRESULT), '"',
dbms_lob.instr(UPPER(a.LINETESTRESULT), 'LHCRAG') + 11) -
(dbms_lob.instr(UPPER(a.LINETESTRESULT), 'LHCRAG') + 11),
dbms_lob.instr(UPPER(a.LINETESTRESULT), 'LHCRAG') + 11)
AS REFRESH_RAG
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
I'm trying to create a csv file that is a set length, and therefore, the fields are a specific length. I'm trying the get my sql statement to pad these columns for me.
I've tried substring, left, space(xx), right, reverse, converting 'TEST' to a char value and I can't get this to work. I tried reverse knowing that using right (space(20) + 'TEST') got what I wanted but obviously was padded on the wrong side.
It seems that SQL server is just dropping the padded spaces on the right side of my string. The two below won't work. The length always comes back # 4.
select len(reverse(right(space(20) + reverse('TEST'), 20)))
select len(left('TEST' + space(20), 20))
This one does work. But again, the spaces are on the wrong side.
select len(right(space(20) + 'TEST', 20))
This works too, and returns "TEST 1"
select len(left('TEST' + ' 1', 20))
Again, it seems that as long as there is some real text on the right side of the string, it's fine.
Anyone have some ideas about how to make this work?
That is because LEN ignores right padded spaces. You should use datalength instead.
select datalength(reverse(right(space(20) + reverse('TEST'), 20)))
select datalength(left('TEST' + space(20), 20))
According to the documentation for LEN:
Returns the number of characters of the specified string expression,
excluding trailing blanks.
Thanks Sean Lange for pointing out the DATALENGTH method.
select
length = len('TEST' + space(20)),
datalength = datalength('TEST' + space(20)),
string = left('TEST' + space(20), 20)
will give the following output:
length datalength string
----------- ----------- --------------------
4 24 TEST
So as you can see the string has been padded up to 20 with blanks (or actually up to 24, but you only return 20 with LEFT), even though LEN will report length as 4.
I have a varchar column with Url's with data that looks like this:
http://google.mews.......http://www.somesite.com
I want to get rid of the first http and keep the second one so the row above would result in:
http://www.somesite.com
I've tried using split() but can't get it to work
Thanks
If you are trying to do this using T-SQL, you can try something in the lines of:
-- assume #v is the variable holding the URL
SELECT SUBSTRING(#v, PATINDEX('%_http://%', #v) + 1, LEN(#v))
This will return the start position of the first http:// that has before it at least one character (hence the '%_' before it and the + 1 offset).
If the first URL always starts right from the beginning of the string, you can use SUBSTRING() & CHARINDEX():
SELECT SUBSTRING(column, CHARINDEX('http://', column, 2), LEN(column))
FROM table
CHARINDEX simply searches a string for a substring and returns the substring's starting position within the string. Its third argument is optional and, if set, specifies the search starting position, in this case it's 2 so it didn't hit the first http://.