The forum will experience an outage sometime between February 10 at 7:00pm EST and February 12 at 11:59 EST for installation of security updates. The actual time and duration of the outage are unknown but attempts will be made to minimize the downtime. We apologize for any inconvenience.

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
345182339
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.3k9129262
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: 176 times

last updated: 18 Jan '16, 11:49