Crop a sql server string

Asked

Viewed 169 times

0

How can I cut part of a string, for example : "2017/12/teste.jpg", I only need to pick up "test.jpg".

1 answer

1

This command will take everything that is afterward of the latter /

select right(minhaString, charIndex('/', reverse(minhaString) + '_') - 1)

This will take everything that is before of the latter /

select left(minhaString, len(minhaString) - charindex('/', reverse(minhaString) + '/'))

EDIT

And to always pick up from the 9th character:

substring(@string, 9, len(@string))
  • 1

    Thanks, I didn’t know about CHARINDEX. I did it like this: "SUBSTRING(@string, 9, CHARINDEX(RIGHT(@string,1), @string))"

  • Good. You can also use this way: substring(@string, 9, len(@string)), becomes a little simpler to read. :)

  • To conclude this post, post the answer and signal as correct or signal this response if it has served.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.