SQL Substring code to deal with extra characters - sql

I have a string in the format 'Filename_30062021_095700.txt' and I wrote some SQL to get the date bit (in the format DDMMYYYY after the first underscore) then convert to an INT:
declare #filename varchar(50), #filename2 varchar(50)
Set #filename = 'Filename_240621_122110.txt'
Set #filename2 = 'Filename_240621_122110_1.txt'
Select CAST(SUBSTRING(#filename, CHARINDEX('_', #filename) + 1, CHARINDEX('.', #filename) - CHARINDEX('_', #filename) - 1) AS Int) As IntDateFilename1
The problem I have is where the filename randomly has an _1 character at the end before the file extension.
I can't see what to do to my query that would cope with the extra '_1'. I've written something to check the number of _ underscore characters and if there are three then I could do something differently, I just can't see what to do for the best.
I thought of more or less the same query, except for a Left(#filename, 15) + '.txt' instead of #filename but is there a better solution in case the DD or MM ever appear as one digit?

I would use the 2 surrounding underscores as the markers here:
SELECT
SUBSTRING(filename, CHARINDEX('_', filename) + 1,
CHARINDEX('_', filename, CHARINDEX('_', filename) + 1) -
CHARINDEX('_', filename) - 1)
FROM yourTable;
Demo

A little trick you can do to deal with your _1 issue in particular is to work with the string reversed, then charindex gives you the correct character postitions and copes equally well with _1234 etc
Set #filename = 'Filename_240621_122110_1234.txt'
select Reverse(Stuff(Reverse(#filename),(1+CharIndex('.',Reverse(#filename))),( CharIndex('_',Reverse(#filename))-CharIndex('.',Reverse(#filename)) ),''))

I would just look for '_' followed by six digits. Then remove the initial part of the string and take 6 digits:
SELECT LEFT(STUFF(filename, 1, PATINDEX('%_[0-9][0-9][0-9][0-9][0-9][0-9]%', filename), ''), 6)
FROM t ;
Or if you prefer using SUBSTRING():
SUBSTRING(filename, PATINDEX('%_[0-9][0-9][0-9][0-9][0-9][0-9]%', filename) + 1, 6)
Here is a db<>fiddle.

Related

SQL How to extract Middle Characters in a string

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);

How can I use SQL Substring to extract characters from this filename?

I'm attempting to use the SUBSTRING function to extract a name out of a filename.
An example filename would be: "73186_RHIMagnesita_PHI_StopLoss_TruncSSN_NonRedact_Inc_to_Apr2022_Paid_to_Apr2022_EDIT"
I'm attempting to extract the "RHIMagnesita" from this filename.
The substring I used was:
SUBSTRING(DFH.FileName, CHARINDEX('_', DFH.FileName) + 1, CHARINDEX('_PHI', DFH.FileName) - 1)
The results it gave were: "RHIMagnesita_PHI_S"
How do I extract only "RHIMagnesita" using the Substring function?
The third parameter in SUBSTRING is length not position, so you would need to substract the length of the beginning string.
SUBSTRING(DFH.FileName, CHARINDEX('_', DFH.FileName) + 1, CHARINDEX('_PHI', DFH.FileName) - CHARINDEX('_', DFH.FileName))
You might need to add or substract 1, but that's the idea.
You were close. You need to use CHARINDEX to also find the position of the second underscore.
SELECT SUBSTRING(FileName,
CHARINDEX('_', FileName) + 1,
CHARINDEX('_', FileName, CHARINDEX('_', FileName) + 1) -
CHARINDEX('_', FileName) - 1) AS FilePart
FROM yourTable;
Here's a way using STRING_SPLIT and FETCH, rather than SUBSTRING We split the string and only return the second row
SELECT
value
FROM STRING_SPLIT('73186_RHIMagnesita_PHI_StopLoss_TruncSSN_NonRedact_Inc_to_Apr2022_Paid_to_Apr2022_EDIT','_')
ORDER BY (SELECT NULL)
OFFSET 1 ROWS
FETCH NEXT 1 ROWS ONLY;
Note: On Azure Sql Server STRING_SPLIT has an ordinal parameter, so you could write this
SELECT
value
FROM
STRING_SPLIT('73186_RHIMagnesita_PHI_StopLoss_TruncSSN_NonRedact_Inc_to_Apr2022_Paid_to_Apr2022_EDIT','_', 1)
WHERE ordinal = 2

is there a way to use a mid-style function in sql like you can for excel?

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)

SQL Trim on a file name

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

SQL: how to select a substring between special characters

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. :)