How to replace a string by a subtring in postgres? - postgresql

I have the following file name in a column on my db: /folder/path/24/24_1543250974365.wav
And I want to replace it by: 24_1543250974365.wav
In summary, I need to cut off the path and replace it by what comes after the last "/" in the String.
My idea is to find the last occurrence of "/" and get the string from there to the end and do a replacement, but not sure if that's the best way to do that
UPDATE tablename
SET file = REPLACE(file, file, substring('/', 1))
WHERE id=1;
I'll need a function that replace all data in this column, so I don't have anymore the entire path but only the file name. Any help would be appreciated!

You might have to add or subtract 1, but this should work
right(file,strpos(reverse(file),'/'))

select right(file,position('/' in reverse(file))-1)

Related

Getting Python to accept a csv into postgreSQL table with ":" in the headers

I receive a .csv export every 10 minutes that I'd like to import into a postgreSQL server. Working with a test csv, I got everything to work, but didn't take notice that my actual csv file has a forced ":" at the end of each column header (but not on the first header for some reason)(built into the back-end of the exporter, so I cant get it removed, already asked the company). So I added the ":"s to my test csv as shown in the link,
My insert into functions no longer work and give me syntax errors. First I'm trying to add them using the following code,
print("Reading file contents and copying into table...")
with open('C:\\Users\\admin\\Desktop\\test2.csv') as csvfile:
readCSV = csv.reader(csvfile, delimiter=',')
columns = next(readCSV) #skips the header row
query = 'insert into test({0}) values ({1})'
query = query.format(','.join(columns), ','.join('?' * len(columns)))
for data in readCSV:
cursor.execute(query, data)
con.commit()
Resulting in '42601' error near ":" in the second column header.
The results are the same while actually listing column headers and ? ? ?s out in the INSERT INTO section.
What is the syntax to get the script to accept ":" on column headers? If there's no way, is there a way to scan through headers and remove the ":" at the end of each?
Because : is a special character, if your column is named year: in the DB, you must double quote its name --> select "year:" from test;
You are getting a PG error because you are referencing the unquoted column name (insert into test({0})), so add double quotes there.
query = 'insert into test("year:","day:", "etc:") values (...)'
That being said, it might be simpler to remove every occurrence of : in your csv's 1st line
Much appreciated JGH and Adrian. I went with your suggestion to remove every occurrence of : by adding the following line after the first columns = ... statement
columns = [column.strip(':') for column in columns]
It worked well.

How to Remove File Name with Extension from File Path Field in PostgreSQL

I have an Image path Field in the table and I want to get path without filename and it's an extension.
I tried using the POSITION of the charecture way but didn't get the accurate output.
LEFT("My_Path", POSITION('\' IN REVERSE("My_Path")) - 1)
The file path is something like
\Nikunj\Images\1.png
My expected result is
Nikunj\Images\
Simply use SUBSTRING
select substring (mypath from '(^.*\\)' ) ;
DEMO
Try following example it is working correctly.
SELECT concat(SPLIT_PART('\Nikunj\Images\1.png', '\', 2),'\',SPLIT_PART('\Nikunj\Images\1.png', '\', 3),'\') as c2;
Thank you, friends, I got a solution for that and this is my solution.
concat(RTRIM(REPLACE(My_Path, reverse(substring(reverse(My_Path) from 1 for strpos(reverse(My_Path),'\')-1)),''),'\'),'\') AS VolumePath
This solution may help you in the feature.
Thanks for your answers friends.

The value in "sym" file disappears when using splayed tables

I am using the following line:
`:c:/dir/ set .Q.en[`:c:/dir; tablename]
Everything is ok if I don't exit KDB, but if I do and then try to load the table using
get `dir
all the symbol columns are integer. I would really appreciate your help into understanding why this happens.
It looks like you forgot to repeat the table name on the l.h.s. of set.
Try
q)`:c:/dir/tablename/ set .Q.en[`:c:/dir; tablename]
This will correctly save table columns in c:/dir/tablename subdirectory and place the sym file alongside. Now you should be able to load both your table and the sym file by using the \l command or specifying c:/dir on the command line when you restart q
q c:/dir
or
q
q)\l c:/dir
(no backticks or leading :'s in either of those commands)
If you want to use get on this table, you will have to load sym separately:
q)load`:c:/dir/sym
q)get`:c:/dir/tablename/
(note the leading : in the path specs)
Finally, you may want to take a look at the rsave command which will save your table without you having to write tablename twice.
.Q.en takes 2 oarams - file handle and table data
Your first param isnt a hsym - should be backtick then colon then path to your db root
Also set takes 2 params - first in this case should be the path to where you want to save like dir/splayedTableName/

how to select character varying data properly in postgresql

I tried to select a data which is in column "fileName" and its fileName is '2016-11-22-12-55-09_hyun.png'
I tired the
select * from images where 'fileName' like '2016-11-22-12-55-09_hyun.png'
However it can not select anything, nor has any kind of error info.
How can I select this file with its filename? Thank you so much.
Single quotes denote a string literal. So in this query you aren't evaluating the column filename, but checking whether the string 'filename' is like the string '2016-11-22-12-55-09_hyun.png', which it of course is not. Just drop the quotes from filename and you should be OK. Also note that since you aren't using any wildcards, using the like operator is pretty pointless, and you could (should) just a plain old equality check:
select * from images where fileName = '2016-11-22-12-55-09_hyun.png'
-- No quotes -------------^--------^

T-SQL pathname manipulation

OK so I'm a bit rusty on parts of T-SQL such as string manipulation
I got a string in a field called [path] the value of the field could look like
'foo\Branches\Bar\Baz\Helpers\FileNotFoundResult.cs'
I need to update the field (for all records) so that the result will be
'Branches\Bar\Baz\Helpers\FileNotFoundResult.cs'
that is strip of anything before the first '\' and the '\'
so I'd appricate if any one could help me fill in the "blanks"
UPDATE SourceFiles
SET Path = <blanks>
Something like
UPDATE SourceFiles
SET Path = SUBSTRING(Path,CHARINDEX('\',Path)+1,8000)
(Assuming your Path column isn't a varchar(max))
If your path column might not contain a backslash at all, you might want:
UPDATE SourceFiles
SET Path = CASE
WHEN CHARINDEX('\',Path) = 0 THEN Path
ELSE SUBSTRING(Path,CHARINDEX('\',Path)+1,8000)
END