Pulling variable length substring from middle of string

braindead

I am trying to grab variable length string from a primary string. Example:

ABC*12*1*name name****XX*123456789~
ABC*12*1*diffname diffname****XX*234567890~
ABC*12*1*diffname2 diffname2***XX*345678901~

I need to pull out the 'name name', 'diffname diffname', 'diffname2 diffname2'

etc from the string. And then replace the ' ' between the names with an asterisk - but, I cant just insert in the first space in the string, there could be multiple names, and so I would want to insert the '*' into the second, or third space, depending on the length of the name string.

SELECT 
        CHARINDEX('*1*',data)+3 AS startpos,
        CHARINDEX('***',data) AS Endpos,
        data
from #t
where data like '%ABC*12*1*%'

This gives me a start point and end point for the variable length string. So I try:

SELECT SUBSTRING(data,CHARINDEX('*1*',data)+3,CHARINDEX('***',data) -CHARINDEX('*1*',data)+3)
FROM #t
WHERE data like '%ABC*12*1*name%'

But this gives me

name n name aa*****X

as a result set, basically starting at the start point and then running well past the end point.

What am I doing wrong?

har07

这部分是问题:

SELECT .....-CHARINDEX('*1*',data)+3
FROM   .....
WHERE  .....

您要减去,Endpos因此应该写在方括号中,如下所示:

-(CHARINDEX('*1*',data)+3)

如果除去括号,最后一部分应为-3

-CHARINDEX('*1*',data)-3

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

Check for a substring at the end of string

来自分类Dev

How to validate string length with Mongoose?

来自分类Dev

Variable indel costs for sequences of unequal length

来自分类Dev

Bash: set a shell variable in the middle of the pipeline

来自分类Dev

循环内的java string.Substring StringIndexOutOfBoundsException

来自分类Dev

修改string.Substring

来自分类Dev

Delete a substring from a string with a filename in Perl

来自分类Dev

Draw String in the middle of line drawn by Graphics.DrawLine

来自分类Dev

compute length string of variable with cobol

来自分类Dev

python:How to print the character from a variable with unicode string

来自分类Dev

Substring depending on Delimiter in the string

来自分类Dev

How to execute a GDB command from a string variable?

来自分类Dev

PHP Variable in a string in a string

来自分类Dev

Index and length must refer to a location within the string error in substring

来自分类Dev

Remove a substring from a bash variable

来自分类Dev

Java String length()和substring(int,int)是否不考虑某些字符?

来自分类Dev

What is the global length variable (window.length)?

来自分类Dev

How to print variable length lists as columns in python?

来自分类Dev

splitting a string if a substring has occured

来自分类Dev

String.substring()复制基础char []值

来自分类Dev

C#string.Substring()或string.Remove()

来自分类Dev

Regex pattern to find n non-space characters of x length after a certain substring

来自分类Dev

String.Substring解释

来自分类Dev

C#中string.Split()与string.Substring()的效率?

来自分类Dev

Count number of a substring repetition in a string

来自分类Dev

某些输入时String.Substring()崩溃

来自分类Dev

StringIndexOutOfBoundsException:String.substring()和String.indexOf()问题

来自分类Dev

.NET 到 Delphi(string.Substring 函数)

来自分类Dev

为什么错误 Invalid length parameter传递给 LEFT 或 SUBSTRING 函数?

Related 相关文章

  1. 1

    Check for a substring at the end of string

  2. 2

    How to validate string length with Mongoose?

  3. 3

    Variable indel costs for sequences of unequal length

  4. 4

    Bash: set a shell variable in the middle of the pipeline

  5. 5

    循环内的java string.Substring StringIndexOutOfBoundsException

  6. 6

    修改string.Substring

  7. 7

    Delete a substring from a string with a filename in Perl

  8. 8

    Draw String in the middle of line drawn by Graphics.DrawLine

  9. 9

    compute length string of variable with cobol

  10. 10

    python:How to print the character from a variable with unicode string

  11. 11

    Substring depending on Delimiter in the string

  12. 12

    How to execute a GDB command from a string variable?

  13. 13

    PHP Variable in a string in a string

  14. 14

    Index and length must refer to a location within the string error in substring

  15. 15

    Remove a substring from a bash variable

  16. 16

    Java String length()和substring(int,int)是否不考虑某些字符?

  17. 17

    What is the global length variable (window.length)?

  18. 18

    How to print variable length lists as columns in python?

  19. 19

    splitting a string if a substring has occured

  20. 20

    String.substring()复制基础char []值

  21. 21

    C#string.Substring()或string.Remove()

  22. 22

    Regex pattern to find n non-space characters of x length after a certain substring

  23. 23

    String.Substring解释

  24. 24

    C#中string.Split()与string.Substring()的效率?

  25. 25

    Count number of a substring repetition in a string

  26. 26

    某些输入时String.Substring()崩溃

  27. 27

    StringIndexOutOfBoundsException:String.substring()和String.indexOf()问题

  28. 28

    .NET 到 Delphi(string.Substring 函数)

  29. 29

    为什么错误 Invalid length parameter传递给 LEFT 或 SUBSTRING 函数?

热门标签

归档