Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
stevo_3
Starting Member
20 Posts |
Posted - 2006-01-18 : 13:48:01
|
Hello all,i got a table workitem with a field comment (ntext),content of comment(example):<STIB> 0502R314231 <LOCATION> STIB TOR 15 3e av. <FIRSTNAME> NATHALIE<LASTNAME> HENRY <PHONE>021465656 <FAX> 021565465what i want the number of characters between <FIRSTNAME> and <LASTNAME>, here it would be 9 (space + NATHALIE), is there a function who can do this,i know with charindex you can do a lookup for a string field but it is not exactly what i want, i tested it alreadyanyone has an idea?ThxS  |
|
|
Vivaldi
Constraint Violating Yak Guru
298 Posts |
Posted - 2006-01-18 : 13:58:55
|
| Couldn't you find the CharIndex of the two "tags" and then get the difference? ________________________________________________The only cure for thinking is picking rocks, or drinking beer. |
 |
|
|
stevo_3
Starting Member
20 Posts |
Posted - 2006-01-18 : 14:19:40
|
| hello,you mean something like this: charindex('<FIRSTNAME>',comment,1) - charindex('<LASTNAME>',comment,1)i don't know exactly what charindex is returning? is it returning a int then it is ok, but when it is returning another data type i'll have problems |
 |
|
|
Vivaldi
Constraint Violating Yak Guru
298 Posts |
Posted - 2006-01-18 : 14:26:53
|
quote: Originally posted by stevo_3 hello,you mean something like this: charindex('<FIRSTNAME>',comment,1) - charindex('<LASTNAME>',comment,1)i don't know exactly what charindex is returning? is it returning a int then it is ok, but when it is returning another data type i'll have problems
BOL is your friend (it certainly is mine)CHARINDEXReturns the starting position of the specified expression in a character string. SyntaxCHARINDEX ( expression1 , expression2 [ , start_location ] ) Argumentsexpression1Is an expression containing the sequence of characters to be found. expression1 is an expression of the short character data type category.expression2Is an expression, usually a column searched for the specified sequence. expression2 is of the character string data type category.start_locationIs 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.Return TypesintRemarksIf either expression1 or expression2 is of a Unicode data type (nvarchar or nchar) and the other is not, the other is converted to a Unicode data type.If either expression1 or expression2 is NULL, CHARINDEX returns NULL when the database compatibility level is 70 or later. If the database compatibility level is 65 or earlier, CHARINDEX returns NULL only when both expression1 and expression2 are NULL. If expression1 is not found within expression2, CHARINDEX returns 0.ExamplesThe first code example returns the position at which the sequence "wonderful" begins in the notes column of the titles table. The second example uses the optional start_location parameter to begin looking for wonderful in the fifth character of the notes column. The third example shows the result set when expression1 is not found within expression2.USE pubsGOSELECT CHARINDEX('wonderful', notes)FROM titlesWHERE title_id = 'TC3218'GO-- Use the optional start_location parameter to start searching -- for wonderful starting with the fifth character in the notes-- column.USE pubsGOSELECT CHARINDEX('wonderful', notes, 5)FROM titlesWHERE title_id = 'TC3218'GOHere is the result set for the first and second queries:----------- 46 ________________________________________________The only cure for thinking is picking rocks, or drinking beer. |
 |
|
|
|
|
|
|
|