VB.Net - SQL Query Getting End of Field Data

Good Day Experts!

I have an intriguing request that seems possible to achieve but I am not quite sure how to achieve it.

I have a field in a SQL table called Notes.  In Notes, there may or may not be information pertaining to a Department.  If appearing the Department will vary in length.  Also. there will be varying amount of data in front of the Department.  Here is how it will appear in the data:

Notes:
<Variable amount of text>Department(1)
<Variable amount of text>Department(TG)
<Variable amount of text>Department(456)

I was thinking to work from the end since I cannot be 100% positive the word Department won't appear in the <Variable amount of text>.  

How can I work from the end back until the D in Department?

Thanks,
jimbo999999
Jimbo99999Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Phillip BurtonDirector, Practice Manager and Computing ConsultantCommented:
As it is vb.net you are using, can you use the InstrRev function?
Vitor MontalvãoMSSQL Senior EngineerCommented:
What is the data type of Notes?
Jimbo99999Author Commented:
I tried it in the query and I receive feedback indicating InStrRev is not a recognized function name.
Acronis True Image 2019 just released!

Create a reliable backup. Make sure you always have dependable copies of your data so you can restore your entire system or individual files.

Vitor MontalvãoMSSQL Senior EngineerCommented:
The correspondent in SQL Server is the REVERSE function. But you didn't answer me: what is the data type of Notes?
Brian CroweDatabase AdministratorCommented:
DECLARE @text      VARCHAR(MAX) = '<Variable amount of text>Department(TG123)'

SELECT REVERSE(SUBSTRING(REVERSE(@text), 2, CHARINDEX('(', REVERSE(@text)) - 2))

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
PortletPaulEE Topic AdvisorCommented:
select
   right( [Notefield] ,charindex('D',reverse( [NoteField] )))
from Notes

{+edit}
didn't know the field name so I used [Notefield]
Jimbo99999Author Commented:
Vitor: Thanks for replying. I was sitting on the question typing and did not refresh to see your contribution. I apologize for any confusion.

The DataType for Notes is varchar.

Thanks,
jimbo99999
Vitor MontalvãoMSSQL Senior EngineerCommented:
Good. I was afraid that it would be a TEXT field but if it's Varchar you can perform search in the field with the above functions.
Vitor MontalvãoMSSQL Senior EngineerCommented:
By the way, do you want to return the department name or doesn't matter and you only want to return the rows that has DEPARTMENT in the Notes?
If so, then you can use the '%' wildcard:
SELECT *
FROM TableName
WHERE Notes LIKE '%department%'

Open in new window

Jimbo99999Author Commented:
I am already sitting on the record and have the Notes field.  I am trying to parse it from the field like what Paul is suggesting.  I am trying it now.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.