Need output of substring sql without using reverse - sql

I have a SQL query which is returning output; I need to get substring with last occurrence, I am confused and not able to find out how to do that:
SELECT
spsetuppath,
SUBSTRING(spsetuppath, 0, (LEN(spsetuppath) - CHARINDEX('\', RTRIM(LTRIM(REVERSE(spsetuppath))))) + 1)
FROM
UMRdb..sql_spversion
WHERE
bitversion = '64'
AND productversion = ' 10.50.2500.0'
Output of this command is as follows
For the last column, I need the path before exe and I need only exe file name - how to do that?
So output I am expecting should look like this
SQLServer2008R2SP1-KB2528583-x64-ENU.exe

select right(spsetuppath, charindex('\', reverse(spsetuppath) + '\') - 1) from UMRdb..sql_spversion where bitversion='64' and
productversion=' 10.50.2500.0 '
this worked awesome.

This should help you:
SELECT
spsetuppath,
SUBSTRING(spsetuppath, CHARINDEX('\', RTRIM(LTRIM(REVERSE(spsetuppath)))) - 1, 150)
FROM
UMRdb..sql_spversion
WHERE
bitversion = '64'
AND productversion = ' 10.50.2500.0'
Example:
DECLARE #string varchar(256) = '\\blablabla\dbms\MSSQL\SQL SERVER 2008\SQLServer2008R2SP1-KB2528583-x64-ENU.exe'
SELECT SUBSTRING(#string, CHARINDEX('\', RTRIM(LTRIM(REVERSE(#string)))) - 1, 150)
EDIT: If you want to avoid SUBSTRING function use this:
DECLARE #string varchar(256) = '\\blablabla\dbms\MSSQL\SQL SERVER 2008\SQLServer2008R2SP1-KB2528583-x64-ENU.exe'
SELECT REVERSE(LEFT(REVERSE(#string),CHARINDEX('\', RTRIM(LTRIM(REVERSE(#string)))) - 1))

Related

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

Delete the last part of a string SQL

I have a field named Path and it looks like this:
/{e8cfdcba-9572-4c64-828f-dea54d8a00b7}/sites/BI_Center/euroreporting/Reports/BITraining/Elena/GroupingEx.rdl
I need a parameter from where i can choose a folder name. Something like this:
/sites/BI_Center/euroreporting/Reports/BITraining/Elena
What i have done by now is to delete the first bit of the path. This is the code:
SELECT replace(reverse(substring(reverse(Path), 1, ISNULL(NullIF(charindex('}',reverse(Path)),0),len(Path))) ),'}','') AS Path2 from Catalog
Now, my path looks like this: /sites/BI_Center/euroreporting/Reports/BITraining/Elena/GroupingEx.rdl
How can i exclude the report's name? (for example GroupingEx.rdl). I tried the MID function, but it doesn't work because the report's name length is variable.
Thank you in advance.
This is one of the methods
declare #s varchar(200)
set #s='/sites/BI_Center/euroreporting/Reports/BITraining/Elena/GroupingEx.rdl'
select reverse(replace(reverse(#s),substring(reverse(#s),1,charindex('/',reverse(#s))),''))
EDIT:
This is much simpler
declare #s varchar(200)
set #s='/sites/BI_Center/euroreporting/Reports/BITraining/Elena/GroupingEx.rdl'
select substring(#s,1,len(#s)-charindex('/',reverse(#s)))
I suggest this function:
SUBSTRING([path], CHARINDEX('/', [path], 2),
LEN([path]) - CHARINDEX('/', [path], 2) - CHARINDEX('/', REVERSE([path]), 1) + 1)
for this: /sites/BI_Center/euroreporting/Reports/BITraining/Elena
declare #str varchar(250)
set #str = '/{e8cfdcba-9572-4c64-828f-dea54d8a00b7}/sites/BI_Center/euroreporting/Reports/BITraining/Elena/GroupingEx.rdl'
select substring (
#str, --expression
charindex('}', #str) + 1, --start
len(#str) - charindex('/', reverse(#str)) - charindex('}', #str) --how many characters of the expression will be returned
)
OutPut:
/sites/BI_Center/euroreporting/Reports/BITraining/Elena

SQL SERVER 2008 - Returning a portion of text using SUBSTRING AND CHARINDEX. Need to return all text UNTIL a specific char

I have a column called 'response' that contains lots of data about a person.
I'd like to only return the info after a specific string
But, using the method below I sometimes (when people have <100 IQ) get the | that comes directly after the required number..
I'd like any characters after the'PersonIQ=' but only before the pipe.
I'm not sure of the best way to achieve this.
Query speed is a concern and my idea of nested CASE is likely not the best solution.
Any advice appreciated. Thanks
substring(response,(charindex('PersonIQ=',response)+9),3)
This is my suggestion:
declare #s varchar(200) = 'aaa=bbb|cc=d|PersonIQ=99|e=f|1=2'
declare #iq varchar(10) = 'PersonIQ='
declare #pipe varchar(1) = '|'
select substring(#s,
charindex(#iq, #s) + len(#iq),
charindex(#pipe, #s, charindex(#iq, #s)) - (charindex(#iq, #s) + len(#iq))
)
Instead of the 3 in your formula you should calculate the space between #iq and #pipe with this last part of the formula charindex(#pipe, #s, charindex(#iq, #s)) - (charindex(#iq, #s) + len(#iq)), which gets the first #pipe index after #iq, and then substructs the index of the IQ value.
Assuming there's always a pipe, you could do this:
substring(stuff(reponse,1,charindex('PersonIQ=',reponse)-1,''),1,charindex('|',stuff(reponse,1,charindex('PersonIQ=',reponse)-1,''))-1)
Or, you could convert your string to xml and reference PersonIQ directly, e.g.:
--assuming your string looks something like this..
declare #s varchar(max) = 'asdaf=xxx|PersonIQ=100|xxx=yyy'
select convert(xml, '<x ' + replace(replace(#s, '=', '='''), '|', ''' ') + '''/>').value('(/x/#PersonIQ)[1]','int')

Replacing certain character in email addresses with '*' in an SQL query

From example#gmail.com
exam***#gm***.com
Can anyone mask the email using SQL query.AS show above from example#gmail.com and convert it to
exam***#gm***.com
DECLARE #String VARCHAR(100) = 'example#gmail.com'
SELECT LEFT(#String, 3) + '*****#'
+ REVERSE(LEFT(RIGHT(REVERSE(#String) , CHARINDEX('#', #String) +2), 2))
+ '******'
+ RIGHT(#String, 4)
Result
exa*****#gm******.com
Just thought of another simpler solution
SELECT LEFT(#String, 3) + '*****#'
+ SUBSTRING(#String, CHARINDEX('#',#String)+1,2)
+ '*******'
+ RIGHT(#String, 4)
This will also give you the exact same results.
while insert you can do same from c# code.
try this with other example,
Declare #input varchar(50)='example#gmail.com '
select left(#input,4)+replicate('*',len(substring(#input,5,charindex('#',#input)-5)))
+substring(#input,charindex('#',#input),3)
+replicate('*',len(substring(#input,charindex('#',#input)+3,len(#input)-charindex('.',#input))))
+substring(#input,charindex('.',#input),len(#input))
may be a multi replace works, an alternative masking the vocals:
update table set mail = replace(replace(mail,'a','*') ,'e','*') and go on

Search and Replace Serialized DB Dump

I am moving a database from one server to an other and have lots of serialized data in there. So, I am wondering:
Is it possible to use regex to replace all occurrences like the following (and similar)
s:22:\"http://somedomain.com/\"
s:26:\"http://somedomain.com/abc/\"
s:29:\"http://somedomain.com/abcdef/\"
to
s:27:\"http://someOtherdomain.com/\"
s:31:\"http://someOtherdomain.com/abc/\"
s:34:\"http://someOtherdomain.com/abcdef/\"
If that column, that holds these data, is of the same length, and these occurrences 22, 26, 29,... are at the same position from the beginning of the string. Then, for SQL Server, you can use REPLACE , SUBSTRING with CHARINDEX to do that:
DECLARE #s VARCHAR(50);
DECLARE #sub INT;
SET #s = 's:27:\"http://somedomain.com/\"';
SET #sub = CONVERT(INT, SUBSTRING(#s, CHARINDEX(':', #s) + 1, 2));
SELECT REPLACE(REPLACE(#s, 'somedomain', 'someOtherdomain'), #sub, #sub + 5);
So s:number:\"http://somedomain.com/\" will become s:number + 5:\"http://someOtherdomain.com/\".
If you want to run an UPDATE against that table you can write it this way:
UPDATE #t
SET s = REPLACE(REPLACE(s, 'somedomain', 'someOtherdomain'),
CONVERT(INT, SUBSTRING(s, CHARINDEX(':', s) + 1, 2)),
CONVERT(INT, SUBSTRING(s, CHARINDEX(':', s) + 1, 2)) + 5);
What does this query do, is that, it searches for the occurrence of somedomain and replaces it with someOtherdomain, get the number between the first two :'s, convert it to INT and replace it with the same number + 5. The following is how your data should looks like after you run the previous query:
s:27:\"http://someOtherdomain.com/\"
s:31:\"http://someOtherdomain.com/abc/\"
s:34:\"http://someOtherdomain.com/abcdef/\"
Here is a Live Demo.