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
  1. Home
  2. Database & SysAdmin
  3. Database
  4. substring function

substring function

Scheduled Pinned Locked Moved Database
9 Posts 5 Posters 0 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 @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 i want the following output 12 13 14 But the output is 12 13,14 14 please rectify it

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

    C M 2 Replies Last reply
    0
    • S Sonia Gupta

      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 i want the following output 12 13 14 But the output is 12 13,14 14 please rectify 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
      #2

      Is there any way you can take your input as XML and use OpenXML instead of this nasty string mashing ? '

      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 )

      S 1 Reply Last reply
      0
      • C Christian Graus

        Is there any way you can take your input as XML and use OpenXML instead of this nasty string mashing ? '

        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 )

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

        this is the database side processigs.actually i don;t konw , how long the string can be , 12,13,14,15.......there in the database i am saving these numbers.problem is the code i am using at database end , shows ths following output 12 13,14 14 for the 12,13,14 expression. i wanted 12 13 14 so far xml interaction is not taking place .

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

        K 1 Reply Last reply
        0
        • S Sonia Gupta

          this is the database side processigs.actually i don;t konw , how long the string can be , 12,13,14,15.......there in the database i am saving these numbers.problem is the code i am using at database end , shows ths following output 12 13,14 14 for the 12,13,14 expression. i wanted 12 13 14 so far xml interaction is not taking place .

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

          K Offline
          K Offline
          Kishore P
          wrote on last edited by
          #4

          Can u try the following: -- Code declare @ExpList varchar (1000) , @Delimiter char (1) , @INTValue varchar (20) , @Position int begin create table #Result ( IntValue int ) set @ExpList = '10, 11, 12, 13, 14' set @Delimiter = ',' set @ExpList = ltrim (rtrim (@ExpList)) + @Delimiter set @Position = charindex (@Delimiter, @ExpList, 1) if replace (@ExpList, @Delimiter, '') <> '' begin while @Position > 0 begin set @INTValue = ltrim (rtrim (left (@ExpList, @Position -1))) if @INTValue <> '' begin insert into #Result (IntValue) values (cast (@INTValue as int)) end :) set @ExpList = right (@ExpList, len (@ExpList) - @Position) set @Position = charindex (@Delimiter, @ExpList, 1) end end select * from #Result truncate table #Result drop table #Result end go Sample OUTPUT: -------------- IntValue --------:) 10 11 12 13 14 -- End of Code Kishore.P

          S 1 Reply Last reply
          0
          • K Kishore P

            Can u try the following: -- Code declare @ExpList varchar (1000) , @Delimiter char (1) , @INTValue varchar (20) , @Position int begin create table #Result ( IntValue int ) set @ExpList = '10, 11, 12, 13, 14' set @Delimiter = ',' set @ExpList = ltrim (rtrim (@ExpList)) + @Delimiter set @Position = charindex (@Delimiter, @ExpList, 1) if replace (@ExpList, @Delimiter, '') <> '' begin while @Position > 0 begin set @INTValue = ltrim (rtrim (left (@ExpList, @Position -1))) if @INTValue <> '' begin insert into #Result (IntValue) values (cast (@INTValue as int)) end :) set @ExpList = right (@ExpList, len (@ExpList) - @Position) set @Position = charindex (@Delimiter, @ExpList, 1) end end select * from #Result truncate table #Result drop table #Result end go Sample OUTPUT: -------------- IntValue --------:) 10 11 12 13 14 -- End of Code Kishore.P

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

            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.

            K K 2 Replies Last reply
            0
            • S Sonia Gupta

              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 i want the following output 12 13 14 But the output is 12 13,14 14 please rectify 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
              #6

              http://www.codeproject.com/script/comments/forums.asp?msg=2326409&forumid=12076#xx2326409xx My answer solve your problem???

              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."

              1 Reply Last reply
              0
              • S Sonia Gupta

                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.

                K Offline
                K Offline
                Kishore P
                wrote on last edited by
                #7

                Modified your code, see the below code: declare @exp varchar(100) , @start int , @pos int , @substring varchar(20) set @exp = '12,13,14' --set @pos = 1 set @start = 1 set @pos = charindex(',' , @exp , @start) -- Added here 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 @pos = charindex(',' , @exp , @start) -- Added here set @start = @pos + 1 end go :) Kishore.P

                1 Reply Last reply
                0
                • S Sonia Gupta

                  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.

                  K Offline
                  K Offline
                  Krish KP
                  wrote on last edited by
                  #8

                  Sonia Gupta wrote:

                  print substring(@exp , @start , @pos -1)

                  while executing this statement 1st time, @start is 1 and @pos is 3. so 12 is printed 2nd time, @start is 4 and @pos is 6. so 13,14 is printed i.e. from 4th position 6 characters are printed

                  Regards KP

                  S 1 Reply Last reply
                  0
                  • K Krish KP

                    Sonia Gupta wrote:

                    print substring(@exp , @start , @pos -1)

                    while executing this statement 1st time, @start is 1 and @pos is 3. so 12 is printed 2nd time, @start is 4 and @pos is 6. so 13,14 is printed i.e. from 4th position 6 characters are printed

                    Regards KP

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

                    yes.ThanQ:)

                    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