T-SQL pathname manipulation - tsql

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

Related

How can I break a long string in an "XMLTABLE" embedded SQL statement in RPGLE across multiple lines?

I have an XML path that exceeds 100 characters (and therefore truncates when the source is saved). My statement is something like this:
Exec SQL
Select Whatever
Into :Stuff
From Table as X,
XmlTable(
XmlNamespaces('http://namespace.url/' as "namespacevalue"),
'$X/really/long/path' Passing X.C1 as "X"
Columns
Field1 Char(3) Path 'example1',
Field2 Char(8) Path 'example2',
Field3 Char(32) Path '../example3'
) As R;
I must break $X/really/long/path across multiple lines. Per IBM's documentation,
The plus sign (+) can be used to indicate a continuation of a string constant.
However, this does not even pass precompile ("Token + was not valid"). I suspect this is due to where the string is in the statement.
I have also tried:
Putting the path in a host variable; this was not allowed
Using SQL CONCAT or ||; not allowed
Putting the path in a SQL global variable instead of a host variable; not allowed
I have considered:
Preparing the entire statement, but this is not ideal for a multitude of reasons
Truncating the path at a higher level in the hierarchy, but this does not return the desired "granularity" of records
Is there any way to span this specific literal in an XmlTable function across multiple lines in my source? Thanks for any and all ideas!
Something like
Exec SQL
Select Whatever
Into :Stuff
From Table as X,
XmlTable(
XmlNamespaces('http://namespace.url/' as "namespacevalue"),
'$X/really/+
long/path' Passing X.C1 as "X"
Columns
Field1 Char(3) Path 'example1',
Field2 Char(8) Path 'example2',
Field3 Char(32) Path '../example3'
) As R;
Should work, is that what you tried ?
The + didn't worked for me, so I had to shorten the path with // instead of /, which might by suboptimal .

How to replace a string by a subtring in postgres?

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)

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 -------------^--------^

Get a list of all subdirectories in Matlab

I'm trying get an absolute path of all subfolders in project_dirs.
project_dirs='D:\MPhil\Model_Building\Models\TGFB\Vilar2006\SBML_sh_ver\vilar2006_SBSH_test7\Python_project3_IQM_project';
all_project_dirs=dir(project_dirs)
for i=all_project_dirs,
full_dir=fullfile(project_dirs,i.name)
The above code gives a single string of all the subfolder directories concatenated together. How do I modify my code to get a cell array of these absolute paths?
There's a function for that: genpath(). It will give you all directories recursively in a string, split by :. Use strsplit() to parse the result.
You can do this:
all_project_dirs = {all_project_dirs([all_project_dirs.isdir]).name};
How it works:
This selects, among the elements of all_project_dirs, those that are directories;
From them it gets the name field;
The values of that field are contatenated into a cell array.
You may want to remove the first two directory names, which are always '.' and '..':
all_project_dirs = all_project_dirs(3:end);
To obtain full paths, you can use strcat:
all_project_dirs = strcat(project_dirs, filesep, all_project_dirs);
or, as suggested by Jørgen, use fullfile:
all_project_dirs = fullfile(project_dirs, all_project_dirs);

Name of files in a specific directory using matlab

I want to find the name of files in a specific directory. I know that the dir command return the name of files but it contains the file name with the their extension. Therefore, I used strfind to remove the extension of files as follows:
a = dir(fullfile(dataset_path, [dataset_category '\qrel']))
for i= 3: length(a)
name{i} = a(i).name(1:strfind(a(i).name, '.')-1)
I want a better approach without loop. I wonder is it a way to use vectorization for this purpose. I used the following code but it return an error
a = dir(fullfile(dataset_path, [dataset_category '\qrel']))
name = a.name(1:strfind(a.name, '.')-1)
You can do that with regular expressions:
name = regexprep({a.name}, '\.[^\.]*$', '');
This collects all names in a cell array ({a.name}). For each string it matches a dot (\.) followed by zero or more characters other than a dot ([^\.]*) at the end of the string ($), and removes that. Thanks to #Shai for the "other than a dot" correction, which makes sure that only the final dot is matched.