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. Want to split variable in SQL[modified]

Want to split variable in SQL[modified]

Scheduled Pinned Locked Moved Database
database
5 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.
  • X Offline
    X Offline
    xodeblack
    wrote on last edited by
    #1

    I have variable @keys which value is Declare @keys varchar(max) set @keys = '44,333,234,125,3,3445' I want to split above variable in following from

    44
    333
    234
    125
    3
    3445

    the length of @keys can be 4000 character

    Dre@m is not th@t wh@t u see in sleep.Dre@m is the thing which does not allow u to sleep

    modified on Saturday, September 12, 2009 9:52 AM

    I T A P 4 Replies Last reply
    0
    • X xodeblack

      I have variable @keys which value is Declare @keys varchar(max) set @keys = '44,333,234,125,3,3445' I want to split above variable in following from

      44
      333
      234
      125
      3
      3445

      the length of @keys can be 4000 character

      Dre@m is not th@t wh@t u see in sleep.Dre@m is the thing which does not allow u to sleep

      modified on Saturday, September 12, 2009 9:52 AM

      I Offline
      I Offline
      i i i
      wrote on last edited by
      #2

      DECLARE @NextString NVARCHAR(40) DECLARE @Pos INT DECLARE @NextPos INT DECLARE @String NVARCHAR(40) DECLARE @Delimiter NVARCHAR(40) SET @String ='softdev,sup' SET @Delimiter = ',' SET @String = @String + @Delimiter SET @Pos = charindex(@Delimiter,@String) WHILE (@pos <> 0) BEGIN SET @NextString = substring(@String,1,@Pos - 1) SELECT @NextString -- Show Results SET @String = substring(@String,@pos+1,len(@String)) SET @pos = charindex(@Delimiter,@String) END Result - Softdev - sup

      Best Of Regards, SOFTDEV If you have knowledge, let others light their candles at it

      1 Reply Last reply
      0
      • X xodeblack

        I have variable @keys which value is Declare @keys varchar(max) set @keys = '44,333,234,125,3,3445' I want to split above variable in following from

        44
        333
        234
        125
        3
        3445

        the length of @keys can be 4000 character

        Dre@m is not th@t wh@t u see in sleep.Dre@m is the thing which does not allow u to sleep

        modified on Saturday, September 12, 2009 9:52 AM

        T Offline
        T Offline
        tagyurit
        wrote on last edited by
        #3

        do a string replace: pseudocode: string sLineFeed = "\n"; (or vbCrLf for Visual Basic or <br> for HTML) string sSource = "A,B,C,D,E,F,G"; string sDestintion = replace(sSource,",",sLineFeed) Rob http://tagyurit.com

        r

        1 Reply Last reply
        0
        • X xodeblack

          I have variable @keys which value is Declare @keys varchar(max) set @keys = '44,333,234,125,3,3445' I want to split above variable in following from

          44
          333
          234
          125
          3
          3445

          the length of @keys can be 4000 character

          Dre@m is not th@t wh@t u see in sleep.Dre@m is the thing which does not allow u to sleep

          modified on Saturday, September 12, 2009 9:52 AM

          A Offline
          A Offline
          Abhishek Sur
          wrote on last edited by
          #4

          Use this function :

          CREATE FUNCTION [dbo].[FN_STRINGTOTABLE](@P_STRING VARCHAR(8000), @P_DELIMITER CHAR(1))
          RETURNS @P_TEMPTABLE TABLE
          (ITEMS VARCHAR(8000))
          AS
          BEGIN
          DECLARE @V_IDX INT
          DECLARE @V_SLICE VARCHAR(8000)

          SELECT @V\_IDX = 1        
              IF LEN(@P\_STRING)<1 OR @P\_STRING IS NULL  RETURN        
             
          WHILE @V\_IDX != 0        
          BEGIN        
              SET @V\_IDX = CHARINDEX(@P\_DELIMITER,@P\_STRING)        
              IF @V\_IDX	!=0        
                  SET @V\_SLICE = LEFT(@P\_STRING,@V\_IDX - 1)        
              ELSE        
                  SET @V\_SLICE = @P\_STRING        
                 
              IF(LEN(@V\_SLICE)>0)   
                  INSERT INTO @P\_TEMPTABLE(ITEMS) VALUES(@V\_SLICE)        
          
              SET @P\_STRING = RIGHT(@P\_STRING,LEN(@P\_STRING) - @V\_IDX)        
              IF LEN(@P\_STRING) = 0 BREAK        
          END    
          

          RETURN
          END

          This will work great. :rose:

          Abhishek Sur


          My Latest Articles **Create CLR objects in SQL Server 2005 C# Uncommon Keywords Read/Write Excel using OleDB

          **Don't forget to click "Good Answer" if you like to.

          1 Reply Last reply
          0
          • X xodeblack

            I have variable @keys which value is Declare @keys varchar(max) set @keys = '44,333,234,125,3,3445' I want to split above variable in following from

            44
            333
            234
            125
            3
            3445

            the length of @keys can be 4000 character

            Dre@m is not th@t wh@t u see in sleep.Dre@m is the thing which does not allow u to sleep

            modified on Saturday, September 12, 2009 9:52 AM

            P Offline
            P Offline
            PIEBALDconsult
            wrote on last edited by
            #5

            Why? :wtf:

            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