Nikunj Satasiya
Nikunj Satasiya

Reputation: 1053

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\

Upvotes: 0

Views: 1639

Answers (3)

Kaushik Nayak
Kaushik Nayak

Reputation: 31676

Simply use SUBSTRING

select substring (mypath from '(^.*\\)' ) ;

DEMO

Upvotes: 2

Nikunj Satasiya
Nikunj Satasiya

Reputation: 1053

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.

Upvotes: 0

Afzal Patel
Afzal Patel

Reputation: 144

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;

Upvotes: 0

Related Questions