I have a string that looks like this. I need to extract after the third '/', or to pull out the section called 'Reports' from this string.
/WM/Operational/Reports/ReportName
I have tried the following:
This gives me the last part or the ReportName
= RIGHT(col,charindex('/',reverse(col),1)-1)
this removes the leading characters after the 3rd '/' character (number of characters are different than above string to hide details)
= RIGHT(col, LEN(col) - 31)
How do I combine this together to remove the first part after the 3rd '/' and remove the reportname?
Assuming the format of your example string has no more than 4 parts you could make use of parsename here:
declare #col varchar(50) = '/WM/Operational/Reports/ReportName';
select ParseName(Replace(stuff(#col, 1, 1, ''), '/', '.'), 2);
Related
I'm working with a varchar column in AWS Redshift. Each string in it has at least one hyphen (-).
I want to keep the substring after the first hyphen. For example:
00-11-22-33 -> 11-22-33
00-112233 -> 112233
The solutions to this question do not work when there are multiple hyphens in a string.
For instance:
select
split_part('00-11-22-33', '-', 2) as attempt1 -- returns '11'
, regexp_replace( '00-11-22-33', '.*-', '') as attempt2 -- returns '33'
;
I'm looking for a solution that returns 11-22-33.
If there's always a hyphen, and you always want only what is after the first hyphen, you may be working too hard.
Here's a more brute force approach.
select substring(
'00-11-22-33'
FROM charindex('-', '00-11-22-33') + 1)
)
or
select substring(
'00-11-22-33',
charindex('-', '00-11-22-33') + 1,
len('00-11-22-33') - charindex('-', '00-11-22-33')
)
or
select substring(
'00-11-22-33',
charindex('-', '00-11-22-33') + 1,
len('00-11-22-33')
)
...because it won't return more characters than exist.
You could match until the first hyphen with ^[^-]*-
And replace with an empty string.
regexp_replace('00-11-22-33', '^[^-]*-', '');
If there should be at least a single char after the hyphen, then you can match with this pattern and replace with capture group 1 like '$1' instead of an empty string.
^[^-]*-(.+)
If the char after the hyphen should not be another hyphen, then you can match a single char other than a hyphen and also replace with '$1'
^[^-]*-([^-].*)
I have the following string of characters:
594074_Ally_Financial_TokioMarine_MD_SLDET_20210101_20211130_20211208
I am attempting to extract everything after the first '_' but before the '_TokioMarine', so the final string will look like:
Ally_Financial
Is this possible to do with SQL? I attempted but it was pulling the incorrect characters. I cant get the ones in between the values specified.
SELECT
#CurPolicyHolder = Right( DFH.FileName, CHARINDEX('_', DFH.FileName) - 1)
To extract everything between the first _ character and the _TokyoMarine string, you can use:
SELECT
#CurPolicyHolder = SUBSTRING(DFH.FileName, CHARINDEX('_', DFH.FileName) + 1,
CHARINDEX('_TokioMarine', DFH.FileName) - CHARINDEX('_', DFH.FileName) - 1)
SUBSTRING (Transact-SQL)
CHARINDEX (Transact-SQL)
I am trying to trim a long file name and just want to get the last characters of that file name which is after the last forward slash (\) and I also want to eliminate the .xlsx at the end of the file name as well.
Original:
sasa\sasas\sasas\1234_IASS.xlsx
Expected Output:
1234_IASS
Your comments state that both your file path and file extension are constant. If the number of characters in your file is also constant the simplest solution is to use SUBSTRING.
SELECT SUBSTRING(YourColumn, 18, 9)
FROM YourTable
If the number of characters is changing, a more robust solution is to use RIGHT to extract the file name and REPLACE to remove the file extension.
SELECT REPLACE(RIGHT(YourColumn, LEN(YourColumn) - 17), '.xlsx', '')
FROM YourTable
If you need a more dynamic solution, you can first extract the filename as shown.
SELECT RIGHT(YourColumn, CHARINDEX('\', REVERSE(YourColumn)) - 1)
FROM YourTable
You can then combine this with REPLACE as before to remove the extension.
SELECT REPLACE(RIGHT(YourColumn, CHARINDEX('\', REVERSE(YourColumn)) - 1), '.xlsx', '')
FROM YourTable
You can try this it will work as said in the comment the file extension are fixed.
SELECT
Replace(
RIGHT('\' + RTRIM('sasa\sasas\sasas\1234_IASS.xlsx'), CHARINDEX('\', REVERSE('\' + RTRIM('sasa\sasas\sasas\1234_IASS.xlsx'))) - 1)
,'.xlsx', '')
as FileName
You can find the live example here.
You say your directory is the same and the extension is always the same? Replace [path] with your table column name:
select replace(replace([path],'sasa\sasas\sasas\',''),'.xlsx','')
declare #x varchar(100) = 'sasa\sasas\sasas\1234_IASS.xlsx'
declare #filename varchar(max) = reverse(substring(reverse(#x), 1, charindex('\', reverse(#x))-1 ))
select substring(#filename, 1, charindex('.', #filename)-1)
Your post's title is misleading, TRIM is performed in sql-server to remove whitespace either by RTRIM or LTRIM or a combination of the two, what you are trying to do is get a substring out of your example string, I am providing a solution which uses a combination of REVERSE, SUBSTRING and CHARINDEX, this answer is good for if you ever need to do this for different file extensions:
DECLARE #test varchar(255) = 'sasa\sasas\sasas\1234_IASS.xlsx';
DECLARE #lastOccurance INT = CHARINDEX('\', REVERSE(#test)); --Last occurence of the slash character to denote the end of the directory name or what not
DECLARE #lastPeriod INT = CHARINDEX('.', REVERSE(#test)); --This is the last occurence of the period, denoting the file extension
SET #lastOccurance = LEN(#test) + 1 - #lastOccurance;
SET #lastPeriod = LEN(#test) + 1 - #lastPeriod;
SELECT SUBSTRING(#test, #lastOccurance + 1, #lastPeriod - (#lastOccurance + 1));
If you want to remove extensions from filename then you can try this:
UPDATE TableName
SET FileName = REVERSE(SUBSTRING(REVERSE(FileName),
CHARINDEX('.', REVERSE(FileName)) + 1, 999))
I'm using SQL Server 2008 and I'm trying to trim values that looks like this
DocID
----------------
FOO_1_23_456
FOO1_1_23_4567
I'm trying to make it so it will only give me everything after the first '_'
Result
_1_23_456
_1_23_4567
Right now my query is
select
right(DocIDDocument, LEN(DocID.Document) - 3)) AS NewDocID
which only the trims the first 3 characters, I need it to where it trims everything before the first '_'
Thanks
Use stuff() and charindex():
select stuff(document, 1, charindex('_', document) - 1, '')
My string looks something like this:
\\\abcde\fghijl\akjfljadf\\
\\xyz\123
I want to select everything between the 1st set and next set of slashes
Desired result:
abcde
xyz
EDITED: To clarify, the special character is always slashes - but the leading characters are not constant, sometimes there are 3 slashes and other times there are only 2 slashes, followed by texts, and then followed by 1 or more slashes, some more texts, 1 or more slash, so on and so forth. I'm not using any adapter at all, just looking for a way to select this substring in my SQL query
Please advise.
Thanks in advance.
You could do a cross join to find the second position of the backslash. And then, use substring function to get the string between 2nd and 3rd backslash of the text like this:
SELECT substring(string, 3, (P2.Pos - 2)) AS new_string
FROM strings
CROSS APPLY (
SELECT (charindex('\', replace(string, '\\', '\')))
) AS P1(Pos)
CROSS APPLY (
SELECT (charindex('\', replace(string, '\\', '\'), P1.Pos + 1))
) AS P2(Pos)
SQL Fiddle Demo
UPDATE
In case, when you have unknown number of backslashes in your string, you could just do something like this:
DECLARE #string VARCHAR(255) = '\\\abcde\fghijl\akjfljadf\\'
SELECT left(ltrim(replace(#string, '\', ' ')),
charindex(' ',ltrim(replace(#string, '\', ' ')))-1) AS new_string
SQL Fiddle Demo2
Use substring, like this (only works for the specified pattern of two slashes, characters, then another slash):
declare #str varchar(100) = '\\abcde\cc\xxx'
select substring(#str, 3, charindex('\', #str, 3) - 3)
Replace #str with the column you actually want to search, of course.
The charindex returns the location of the first slash, starting from the 3rd character (i.e. skipping the first two slashes). Then the substring returns the part of your string starting from the 3rd character (again, skipping the first two slashes), and continuing until just before the next slash, as determined by charindex.
Edit: To make this work with different numbers of slashes at the beginning, use patindex with regex to find the first alphanumeric character, instead of hardcoding that it should be the third character. Example:
declare #str varchar(100) = '\\\1Abcde\cc\xxx'
select substring(#str, patindex('%[a-zA-Z0-9]%', #str), charindex('\', #str, patindex('%[a-zA-Z0-9]%', #str)) - patindex('%[a-zA-Z0-9]%', #str))
APH's solution works better if your string always follows the pattern as described. However this will get the text despite the pattern.
declare #str varchar(100) = '\\abcde\fghijl\akjfljadf\\'
declare #srch char(1) = '\'
select
SUBSTRING(#str,
(CHARINDEX(#srch,#str,(CHARINDEX(#srch,#str,1)+1))+1),
CHARINDEX(#srch,#str,(CHARINDEX(#srch,#str,(CHARINDEX(#srch,#str,1)+1))+1))
- (CHARINDEX(#srch,#str,(CHARINDEX(#srch,#str,1)+1))+1)
)
Sorry for the formatting.
Edited to correct user paste error. :)