The T-SQL processes strings like arrays, split strings

Source: Internet
Author: User
T-SQL on the string processing capabilities are relatively weak, such as I want to loop traversal like 1, 2, 3, 4, 5 such strings, if the use of arrays, traversal is very simple, but the T-SQL does not support the array, so it is troublesome to handle it. The following function implements string processing like an array.

1. Split the string by the specified symbol and return the number of elements after the split. The method is very simple, that is, to check the number of separators in the string, and then add one, which is the required result.

  1. Create FunctionGet_strarraylength
  2. (
  3. @ STR varchar (1024 ),-- String to be split
  4. @ Split varchar (10)-- Separator
  5. )
  6. Returns int
  7. As
  8. Begin
  9. Declare@ Location int
  10. Declare@StartInt
  11. Declare@LengthInt
  12. Set@ STR =Ltrim(Rtrim(@ Str ))
  13. Set@ Location = charindex (@ split, @ Str)
  14. Set@Length= 1
  15. While@ Location <> 0
  16. Begin
  17. Set@Start= @ Location + 1
  18. Set@ Location = charindex (@ split, @ STR ,@Start)
  19. Set@Length= @Length+ 1
  20. End
  21. Return@Length
  22. End

Call example: Select DBO. get_strarraylength ('78, 1, 2, 3 ',',')
Return Value: 4

2. Split the string by the specified symbol and return the nth element of the specified index after the split, which is as convenient as an array.

  1. Create FunctionGet_strarraystrofindex
  2. (
  3. @ STR varchar (1024 ),-- String to be split
  4. @ Split varchar (10 ),-- Separator
  5. @IndexInt-- Take the nth Element
  6. )
  7. Returns varchar (1024)
  8. As
  9. Begin
  10. Declare@ Location int
  11. Declare@StartInt
  12. Declare@ Next int
  13. Declare@ Seed int
  14. Set@ STR =Ltrim(Rtrim(@ Str ))
  15. Set@Start= 1
  16. Set@ Next = 1
  17. Set@ Seed = Len (@ split)
  18. Set@ Location = charindex (@ split, @ Str)
  19. While@ Location <> 0And@Index> @ Next
  20. Begin
  21. Set@Start= @ Location + @ Seed
  22. Set@ Location = charindex (@ split, @ STR ,@Start)
  23. Set@ Next = @ next + 1
  24. End
  25. If@ Location = 0Select@ Location = Len (@ Str) + 1
  26. -- There are two cases: 1. There is no Separator in the string. 2. There is a separator in the string. After jumping out of the while loop, @ location is 0, by default, there is a separator behind the string.
  27. ReturnSubstring (@ STR ,@Start, @ Location -@Start)
  28. End

Call example: Select DBO. get_strarraystrofindex ('8, 9, 4', ',', 2)
Return Value: 9

3. Combine the above two functions to traverse the elements in the string like an array

    1. Declare@ STR varchar (50)
    2. Set@ STR ='1, 2, 3, 4, 5'
    3. Declare@ Next int
    4. Set@ Next = 1
    5. While@ Next <= DBO. get_strarraylength (@ STR,',')
    6. Begin
    7. Print DBO. get_strarraystrofindex (@ STR,',', @ Next)
    8. Set@ Next = @ next + 1
    9. End

Call result:
1
2
3
4
5

http: // www.616.cn HTTP: // game.616.cn
flex web board game, video conversion software, crawlers are welcome to buy them! Slide the internet!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.