Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How can I Find a string in the value of a field? 1

Status
Not open for further replies.

imendi

Programmer
Oct 14, 2002
33
ES
Hello,

I have a SQL that I want to send from VB6 to an SQL Server Database.
Before I was using an Access ddbb, and I had something like:

IIf(InStr([Name],&quot;/&quot;)<>0 And InStr(InStr([Name],&quot;/&quot;)+1,[Name],&quot;/&quot;)<>0 ,
Mid([Name],InStr([Name],&quot;/&quot;)+1,InStr(InStr([Name],&quot;/&quot;)+1, [Name],&quot;/&quot;)-InStr([Name],&quot;/&quot;)-1),0) AS FirstName

That is, I find the position of the first ocurrence of &quot;/&quot; withing the value of the name and then take a number of caracters between the first &quot;/&quot; and the second &quot;/&quot;.

The question is: How can I find a string (i.e. &quot;/&quot;) with SQL Server ?
I want to find the position of the first &quot;/&quot; and then
I would use SUBSTRING to get the caracters.

Sorry if it was a too long explanation.
Many thanks!


imendi

 
Sorry, I forgot.
The value of the field Name could be somthing like:
imendi/IM/smith

 
The function you need is CHARINDEX
from BOL:

CHARINDEX ( expression1 , expression2 [ , start_location ] )

Arguments
expression1

Is an expression containing the sequence of characters to be found. expression1 is an expression of the short character data type category.

expression2

Is an expression, usually a column searched for the specified sequence. expression2 is of the character string data type category.

start_location

Is the character position to start searching for expression1 in expression2. If start_location is not given, is a negative number, or is zero, the search starts at the beginning of expression2.

 
Uf !!

That is what I call an extremely quick answer !!

Many thanks!!

imendi
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top