1

I have a variable which is a VARCHAR and will contain a list of folders (1 or more). It may looks like that :

@Folder = '/folder1/folder2/'
or
@Folder = 'folder1/folder2/'
or
@Folder = '/Folder1/Folder2'

And if the first and the last characater of my string are '/', i want to remove them and my variable should be always

@Folder = 'Folder1/Folder2'

Thanks for your help

3

You could use the LEFT() and RIGHT() functions to do this.

DECLARE @Folder varchar(20) = '/folder1/folder2/';

IF LEFT(@folder,1) = '/' 
SET @Folder = RIGHT(@Folder ,LEN(@Folder)-1);

IF RIGHT(@folder,1) = '/' 
SET @Folder = LEFT(@Folder ,LEN(@Folder)-1);

SELECT @Folder;

Result

(No column name)
folder1/folder2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.