This is how I do it today, but I suspect there are better ways to do it?

BEGIN

declare ls_path nvarchar(200);

set ls_path = 'C:\tempfolder\test\testfile.txt';

select 
    left(ls_path, len(ls_path) -len(row_value) ) cc_path, row_value cc_filename
from 
    sa_split_list(ls_path, '\')
where 
    row_value <> '' and
    line_num = (select max(line_num) from sa_split_list(ls_path, '\') );

END

asked 18 Jan '16, 11:20

Rolle's gravatar image

Rolle
375213041
accept rate: 0%


I would think it is easier to use locate( ..., '\\', -1 ) to find the last backslash and then split the string at that location.

begin
  declare @path varchar(255) = 'c:\\my\\path\\name\\filename.jpg';
  select locate( @path, '\\', -1 ) as len,
         left( @path, len-1 ) as path,
         substr( @path, len+1 ) as filename;
end;

Note that backslashes should be doubled ... otherwise sequences like \n will get interpreted as newline (similar for \r and \t and a few others)

HTH

permanent link

answered 18 Jan '16, 11:44

Mark%20Culp's gravatar image

Mark Culp
22.7k9129266
accept rate: 40%

edited 18 Jan '16, 11:49

Wow, was just about to post something comparable but had to lookup the third LOCATE() parameter, as usual:)

(18 Jan '16, 11:46) Volker Barth
Your answer
toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported

Question tags:

×12

question asked: 18 Jan '16, 11:20

question was seen: 213 times

last updated: 18 Jan '16, 11:49