Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to create function for Split string in SQL

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 570
    Comment on it

    How to create function for split string in SQL

    Function accepts two parameters first the concatenated string and second the delimiter character.

    CREATE FUNCTION [dbo].[Split](@String nvarchar(4000),@Delimiter char(1))
    RETURNS @Results TABLE (Items nvarchar(4000))
    AS
    BEGIN
    DECLARE @IND INT
    DECLARE @SLICE nvarchar(4000)
      SELECT @IND = 1
      IF @String IS NULL RETURN
      WHILE @IND !=0
        BEGIN 
         SELECT @IND = CHARINDEX(@Delimiter,@STRING)
         IF @IND !=0
          SELECT @SLICE = LEFT(@STRING,@IND-1)
         ELSE
          SELECT @SLICE = @STRING
    
         INSERT INTO @Results(Items) VALUES(@SLICE)
         SELECT @STRING = RIGHT(@STRING,LEN(@STRING)-@IND)
         IF LEN(@STRING) = 0 BREAK
    END
    RETURN
    END
    

    How to execute above function

    select items from dbo.split('Split string by space','')
    

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: