Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. Web Development
  3. ASP.NET
  4. substring function

substring function

Scheduled Pinned Locked Moved ASP.NET
9 Posts 5 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Sonia Gupta
    wrote on last edited by
    #1

    declare @exp varchar(100) set @exp = '12,13,14' declare @start int declare @substring varchar(20) set @start = 1 while(@start <> 0) begin set @start = charindex(',' , @exp , @start) if(@start = 0) return print @start --print substring(@exp , @start , set @start = @start + 1 end i want the following output 12 13 14 how should i get it

    Yesterday is a canceled check. Tomorrow is a promissory note. Today is the ready cash. USE IT.

    V C M I 4 Replies Last reply
    0
    • S Sonia Gupta

      declare @exp varchar(100) set @exp = '12,13,14' declare @start int declare @substring varchar(20) set @start = 1 while(@start <> 0) begin set @start = charindex(',' , @exp , @start) if(@start = 0) return print @start --print substring(@exp , @start , set @start = @start + 1 end i want the following output 12 13 14 how should i get it

      Yesterday is a canceled check. Tomorrow is a promissory note. Today is the ready cash. USE IT.

      V Offline
      V Offline
      Vasudevan Deepak Kumar
      wrote on last edited by
      #2

      Sonia, Did you consider posting in SQL/ADO/ADO.NET forum which is a more befiting place for this query than here? http://www.codeproject.com/script/comments/forums.asp?forumid=1725[^]

      Vasudevan Deepak Kumar Personal Homepage
      Tech Gossips
      A pessimist sees only the dark side of the clouds, and mopes; a philosopher sees both sides, and shrugs; an optimist doesn't see the clouds at all - he's walking on them. --Leonard Louis Levinson

      S 1 Reply Last reply
      0
      • S Sonia Gupta

        declare @exp varchar(100) set @exp = '12,13,14' declare @start int declare @substring varchar(20) set @start = 1 while(@start <> 0) begin set @start = charindex(',' , @exp , @start) if(@start = 0) return print @start --print substring(@exp , @start , set @start = @start + 1 end i want the following output 12 13 14 how should i get it

        Yesterday is a canceled check. Tomorrow is a promissory note. Today is the ready cash. USE IT.

        C Offline
        C Offline
        Christian Graus
        wrote on last edited by
        #3

        One good way is to pass XML and use OpenXML instead of string mashing. But, it's true you're in the wrong forum.

        Christian Graus - Microsoft MVP - C++ "also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )

        1 Reply Last reply
        0
        • S Sonia Gupta

          declare @exp varchar(100) set @exp = '12,13,14' declare @start int declare @substring varchar(20) set @start = 1 while(@start <> 0) begin set @start = charindex(',' , @exp , @start) if(@start = 0) return print @start --print substring(@exp , @start , set @start = @start + 1 end i want the following output 12 13 14 how should i get it

          Yesterday is a canceled check. Tomorrow is a promissory note. Today is the ready cash. USE IT.

          M Offline
          M Offline
          Michael Sync
          wrote on last edited by
          #4

          Check-out this SQL split function http://www.devx.com/tips/Tip/20009 Here is my modified (0.1%) code. DECLARE @String nvarchar (4000) DECLARE @Delimiter nvarchar (10) SET @String = '12,13,14' SET @Delimiter = ',' declare @NextString nvarchar(4000) declare @Pos int declare @NextPos int declare @CommaCheck nvarchar(1) --Initialize set @NextString = '' set @CommaCheck = right(@String,1) --Check for trailing Comma, if not exists, INSERT --if (@CommaCheck <> @Delimiter ) set @String = @String + @Delimiter --Get position of first Comma set @Pos = charindex(@Delimiter,@String) set @NextPos = 1 --Loop while there is still a comma in the String of levels while (@pos <> 0) begin set @NextString = substring(@String,1,@Pos - 1) PRINT @NextString set @String = substring(@String,@pos +1,len(@String)) set @NextPos = @Pos set @pos = charindex(@Delimiter,@String) end BTW, this is ASP.NET forum. (You have been here for long time so you know that, right? ):)

          Thanks and Regards, Michael Sync ( Blog: http://michaelsync.net) "Please vote to let me (and others) know if this answer helped you or not. A 5 vote tells people that your question has been answered successfully and that I've pitched it at just the right level. Thanks."

          S 1 Reply Last reply
          0
          • M Michael Sync

            Check-out this SQL split function http://www.devx.com/tips/Tip/20009 Here is my modified (0.1%) code. DECLARE @String nvarchar (4000) DECLARE @Delimiter nvarchar (10) SET @String = '12,13,14' SET @Delimiter = ',' declare @NextString nvarchar(4000) declare @Pos int declare @NextPos int declare @CommaCheck nvarchar(1) --Initialize set @NextString = '' set @CommaCheck = right(@String,1) --Check for trailing Comma, if not exists, INSERT --if (@CommaCheck <> @Delimiter ) set @String = @String + @Delimiter --Get position of first Comma set @Pos = charindex(@Delimiter,@String) set @NextPos = 1 --Loop while there is still a comma in the String of levels while (@pos <> 0) begin set @NextString = substring(@String,1,@Pos - 1) PRINT @NextString set @String = substring(@String,@pos +1,len(@String)) set @NextPos = @Pos set @pos = charindex(@Delimiter,@String) end BTW, this is ASP.NET forum. (You have been here for long time so you know that, right? ):)

            Thanks and Regards, Michael Sync ( Blog: http://michaelsync.net) "Please vote to let me (and others) know if this answer helped you or not. A 5 vote tells people that your question has been answered successfully and that I've pitched it at just the right level. Thanks."

            S Offline
            S Offline
            Sonia Gupta
            wrote on last edited by
            #5

            http://www.codeproject.com/script/comments/forums.asp?msg=2326415&forumid=1725#xx2326415xx[^]

            Yesterday is a canceled check. Tomorrow is a promissory note. Today is the ready cash. USE IT.

            1 Reply Last reply
            0
            • V Vasudevan Deepak Kumar

              Sonia, Did you consider posting in SQL/ADO/ADO.NET forum which is a more befiting place for this query than here? http://www.codeproject.com/script/comments/forums.asp?forumid=1725[^]

              Vasudevan Deepak Kumar Personal Homepage
              Tech Gossips
              A pessimist sees only the dark side of the clouds, and mopes; a philosopher sees both sides, and shrugs; an optimist doesn't see the clouds at all - he's walking on them. --Leonard Louis Levinson

              S Offline
              S Offline
              Sonia Gupta
              wrote on last edited by
              #6

              http://www.codeproject.com/script/comments/forums.asp?msg=2326415&forumid=1725#xx2326415xx[^]

              Yesterday is a canceled check. Tomorrow is a promissory note. Today is the ready cash. USE IT.

              1 Reply Last reply
              0
              • S Sonia Gupta

                declare @exp varchar(100) set @exp = '12,13,14' declare @start int declare @substring varchar(20) set @start = 1 while(@start <> 0) begin set @start = charindex(',' , @exp , @start) if(@start = 0) return print @start --print substring(@exp , @start , set @start = @start + 1 end i want the following output 12 13 14 how should i get it

                Yesterday is a canceled check. Tomorrow is a promissory note. Today is the ready cash. USE IT.

                I Offline
                I Offline
                InsDev
                wrote on last edited by
                #7

                declare @exp varchar(100) set @exp = '12,13,14' declare @start int declare @substring varchar(20) set @start = 1 while(@start <> 0) begin set @start = charindex(',' , @exp , @start) if(@start = 0) Begin select substring(@exp,@start+1,2) return end select substring(@exp,@start+1,2) set @start = @start + 1 end Devjit Das.

                S 2 Replies Last reply
                0
                • I InsDev

                  declare @exp varchar(100) set @exp = '12,13,14' declare @start int declare @substring varchar(20) set @start = 1 while(@start <> 0) begin set @start = charindex(',' , @exp , @start) if(@start = 0) Begin select substring(@exp,@start+1,2) return end select substring(@exp,@start+1,2) set @start = @start + 1 end Devjit Das.

                  S Offline
                  S Offline
                  Sonia Gupta
                  wrote on last edited by
                  #8

                  InsDev wrote:

                  if(@start = 0) Begin select substring(@exp,@start+1,2) return end

                  it is not certain wherther the length will be = 2 or more then 2 if i replace 2 with len(@exp) again bug starts to occur

                  Yesterday is a canceled check. Tomorrow is a promissory note. Today is the ready cash. USE IT.

                  1 Reply Last reply
                  0
                  • I InsDev

                    declare @exp varchar(100) set @exp = '12,13,14' declare @start int declare @substring varchar(20) set @start = 1 while(@start <> 0) begin set @start = charindex(',' , @exp , @start) if(@start = 0) Begin select substring(@exp,@start+1,2) return end select substring(@exp,@start+1,2) set @start = @start + 1 end Devjit Das.

                    S Offline
                    S Offline
                    Sonia Gupta
                    wrote on last edited by
                    #9

                    What's the problem in the following declare @exp varchar(100) set @exp = '12,13,14' declare @start int declare @pos int declare @substring varchar(20) set @pos = 1 set @start = 1 while(@pos <> 0) begin set @pos = charindex(',' , @exp , @start) if(@pos = 0) begin print substring(@exp , @start , len(@exp)) return end print substring(@exp , @start , @pos -1) set @start = @pos + 1 end

                    Yesterday is a canceled check. Tomorrow is a promissory note. Today is the ready cash. USE IT.

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups