Delete the last part of a string SQL - 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

Related

Remove characters before and after string SQL

I would like to remove all characters before and after a string in the select statement.
In the example below I would like to remove everything before and including /Supply> and after and including >/
Note the remaining part will be a fixed number of characters.
Any help would be much appreciated
Eg.
abs/Supply>hhfhjgglldppprrr>/llllllldsfsjhfhhhfdhudfhfhdhdfhfsd
Would become:
hhfhjgglldppprrr
If your input always has exactly two instances of ">" you could use PARSENAME.
declare #SomeValue varchar(100) = 'abs/Supply>hhfhjgglldppprrr>/llllllldsfsjhfhhhfdhudfhfhdhdfhfsd'
select PARSENAME(replace(#SomeValue, '>', '.'), 2)
This will not work correctly if your data also contains any periods (.). We can deal with that if needed with a couple of replace statements. Still very simple and easy to maintain with the same caveat of exactly 2 >.
declare #SomeOtherValue varchar(100) = 'abs/Supply>hhfhjgg.lldppprrr>/llllllldsfsjhfhhhfdhudfhfhdhdfhfsd'
select replace(PARSENAME(replace(replace(#SomeOtherValue, '.', '~!##'), '>', '.'), 2), '~!##', '.')
You can use PATINDEX() to identify the position of the patterns you are looking for (/Supply> and >/) then remove them based on the length of the string:
SELECT LEFT(RIGHT(col,LEN(col) - PATINDEX('%/Supply>%',col) -7), PATINDEX('%>/%', RIGHT(col,LEN(col) - PATINDEX('%Supply>%',col) -7))-1)
Simply replace col in the above with your column name.
Example below with test string abs/Supply>keep>/remove
First remove everything before and including /Supply>:
SELECT RIGHT('abs/Supply>keep>/remove',LEN('abs/Supply>keep>/remove') - PATINDEX('%/Supply>%','abs/Supply>keep>/remove') -7)
This will give keep>/remove
Then remove everything after and including >/:
SELECT LEFT('keep>/remove',PATINDEX('%>/%','keep>/remove') - 1)
This will give keep, the part of the string you want.
Here is the combined version, same as above, just includes the test string instead of col so you can run it easily:
SELECT LEFT(RIGHT('abs/Supply>keep>/remove',LEN('abs/Supply>keep>/remove') - PATINDEX('%/Supply>%','abs/Supply>keep>/remove') -7), PATINDEX('%>/%', RIGHT('abs/Supply>keep>/remove',LEN('abs/Supply>keep>/remove') - PATINDEX('%/Supply>%','abs/Supply>keep>/remove') -7))-1)
This will give keep. You can also replace the string above with the one in your question, I just used a different test string because it is shorter and makes the code more readable.
try this:
DECLARE #inputStr VARCHAR(max)= 'abs/Supply>hhfhjgglldppprrr>/llllllldsfsjhfhhhfdhudfhfhdhdfhfsd'
DECLARE #startString VARCHAR(100)='/Supply>'
DECLARE #EndString VARCHAR(100)='>/'
DECLARE #LenStartString INT = LEN(#startString)
DECLARe #TempInputString VARCHAR(max)='';
DECLARE #StartIndex INT
DECLARE #EndIndex INT
SELECT #StartIndex = CHARINDEX(#startString,#inputStr)+#LenStartString
SELECT #TempInputString = STUFF(#inputStr, 1, #StartIndex, '')
SELECT SUBSTRING(#TempInputString,0,CHARINDEX(#EndString,#TempInputString))
In Single Line
DECLARE #inputStr VARCHAR(max)= 'abs/Supply>hhfhjgglldppprrr>/llllllldsfsjhfhhhfdhudfhfhdhdfhfsd'
DECLARE #startString VARCHAR(100)='/Supply>'
DECLARE #EndString VARCHAR(100)='>/'
SELECT SUBSTRING(STUFF(#inputStr, 1, CHARINDEX(#startString,#inputStr)+LEN(#startString), ''),0,CHARINDEX(#EndString,STUFF(#inputStr, 1,CHARINDEX(#startString,#inputStr)+LEN(#startString), '')))

Need output of substring sql without using reverse

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

Separate a string in SQL in two parts separated by (hyphen) - and store the two parts in a different variable

I need to separate a string in SQL,
Example,
a='001A01-001A05'
What I need in OUTPUT is,
x='001A01'
y='001A05'
Use LEFT, CHARINDEX and SUBSTRING
DECLARE #char VARCHAR(500)= '001A01-001A05'
SELECT LEFT(#char, Charindex('-', #char) - 1),
Substring(#char, Charindex('-', #char) + 1, Len(#char))
Prdp's answer would be my first choice, but if SQL Server 2012+, another option is PARSENAME()
Declare #a varchar(25)='001A01-001A05'
Select x=ParseName(Replace(#a,'-','.'),2)
,y=ParseName(Replace(#a,'-','.'),1)
Returns
x y
001A01 001A05
try this using LEFT and RIGHT
DECLARE #str varchar(max)= '001A01-001A05'
select left(#str, charindex('-', #str)-1),
right(#str, charindex('-', #str)-1)

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

T-SQL SUBSTRING at certain places

I have the following example.
DECLARE #String varchar(100) = 'GAME_20131011_Set - SET_20131012_Game'
SELECT SUBSTRING(#String,0,CHARINDEX('_',#String))
SELECT SUBSTRING(#String,CHARINDEX('- ',#STRING),CHARINDEX('_',#STRING))
I want to get the words 'GAME' and 'SET' (the first word before the first '_' from both sides of ' - '.
I am getting 'GAME' but having trouble with 'SET'
UPDATE: 'GAME' and 'SET' are just examples, those words may vary.
DECLARE #String1 varchar(100) = 'GAMEE_20131011_Set - SET_20131012_Game' -- Looking for 'GAME' and 'SET'
DECLARE #String2 varchar(100) = 'GAMEE_20131011_Set - SETT_20131012_Game' -- Looking for 'GAMEE' and 'SETT'
DECLARE #String2 varchar(100) = 'GAMEEEEE_20131011_Set - SETTEEEEEEEE_20131012_Game' -- Looking for 'GAMEEEEE' and 'SETTEEEEEEEE'
As long as your two parts will always be separated be a specific character (- in your example), you could try splitting on that value:
DECLARE #String varchar(100) = 'GAME_20131011_Set - SET_20131012_Game'
DECLARE #Left varchar(100),
#Right varchar(100)
-- split into two strings based on a delimeter
SELECT #Left = RTRIM(SUBSTRING(#String, 0, CHARINDEX('-',#String)))
SELECT #Right = LTRIM(SUBSTRING(#String, CHARINDEX('-',#String)+1, LEN(#String)))
-- handle the strings individually
SELECT SUBSTRING(#Left, 0, CHARINDEX('_', #Left))
SELECT SUBSTRING(#Right, 0, CHARINDEX('_', #Right))
-- Outputs:
-- GAME
-- SET
Here's a SQLFiddle example of this: http://sqlfiddle.com/#!3/d41d8/22594
The issue that you are running into with your original query is that you are specifying CHARINDEX('- ', #String) for your start index, which will include - in any substring starting at that point. Also, with CHARINDEX('_',#STRING) for your length parameter, you will always end up with the index of the first _ character in the string.
By splitting the original string in two, you avoid these problems.
Try this
SELECT SUBSTRING(#String,0,CHARINDEX('_',#String))
SELECT SUBSTRING(#String,CHARINDEX('- ',#STRING)+1, CHARINDEX('_',#STRING)-1)
charindex takes an optional third parameter that says which poistion in the string to start the search from. You could roll this into one statement, but it's easier to read with three
Declare #start int = charindex('-', #string) + 2;
Declare #end int = charindex('_', #string, #start);
Select substring(#string, #start, #end - #start);
Example SQLFiddle