Take the 2-minute tour ×
Database Administrators Stack Exchange is a question and answer site for database professionals who wish to improve their database skills and learn from others in the community. It's 100% free, no registration required.

I think this is a simple question, but google searching for 30 minutes was mostly wasted time as all I can find is matching a variable to a 'randompieceoftext'.

Anyways, suppose I have a local variable called @ServerName. This server name will be something like CCPWIQAUL. I need to match this server name to various path names, which is of the form:

serverName.something.somethingelse.com

These path names are pulled from a database, and will be in the column vManagedEntity.Path

How do I do something like this?

Is @ServerName is IN vManagedEntity.Path?

share|improve this question
add comment

migrated from serverfault.com Jun 8 '12 at 19:52

This question came from our site for professional system and network administrators.

1 Answer

up vote 1 down vote accepted

You can use the CHARINDEX() function to do this:

SELECT vManagedEntity.Path
FROM vManagedEntity.Path
WHERE CHARINDEX(@ServerName,Path) > 0

Or, you can craft a LIKE query:

Assuming you want to do a Starts With match on the Path field:

SELECT vManagedEntity.Path
FROM vManagedEntity.Path
WHERE Path LIKE @ServerName + '%'

If you want to do an Ends With, change to:

SELECT vManagedEntity.Path
FROM vManagedEntity.Path
WHERE Path LIKE '%' + @ServerName

Or, a Contains:

SELECT vManagedEntity.Path
FROM vManagedEntity.Path
WHERE Path LIKE '%' + @ServerName + '%'
share|improve this answer
    
Also if the pattern is always going to be <servername>.something.somethingelse.com you can use LIKE @ServerName + '.%' - this way a search for 'BOB%' won't match both 'BOB.whatever.com' and 'Bobblehead.whatever.com'... –  Aaron Bertrand Jun 9 '12 at 1:24
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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