question

natongm avatar image
natongm asked

Substring and Patindex

I have a table called catalog with the following columns, and I want to find the last occurence of the - in CategoryDescription2 and remove any text preceding it. For example, I want CategoryDescription2 to just have MINOR DEPARTMENT DRY GROCERY instead of MAJOR DEPARTMENT GROCERY-MINOR DEPARTMENT DRY GROCERY. CategoryDescription1 CategoryDescription2 MAJOR DEPARTMENT MAJOR DEPARTMENT GROCERY-MINOR DEPARTMENT DRY GROCERY
substringpatindex
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

1 Answer

·
JohnM avatar image
JohnM answered
Something like this would work: DECLARE @string VARCHAR(100) = 'MAJOR DEPARTMENT GROCERY-MINOR DEPARTMENT DRY GROCERY' SELECT REVERSE(LEFT(REVERSE(@string),CHARINDEX('-',REVERSE(@string),1)-1)) Result: MINOR DEPARTMENT DRY GROCERY You would just use that code in an UPDATE statement to adjust the values accordingly. Hope that helps!
2 comments
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

natongm avatar image natongm commented ·
Thank you JohnM. It helps!
0 Likes 0 ·
JohnM avatar image JohnM commented ·
Very welcome. If that helped to solve the issue, please make sure to mark it as the answer so that others know your question was answered. Thanks!
0 Likes 0 ·

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.