Solved

What is the SQL split command?

Posted on 2010-08-24
3
596 Views
Last Modified: 2012-05-10
What is the SQL split command?  I need to break out a field that is parsed by commas to create 5 fields.  This is coming from a description field in an accounting package.

Thanks!
0
Comment
Question by:kgittinger
  • 2
3 Comments
 
LVL 73

Accepted Solution

by:
sdstuber earned 250 total points
ID: 33510469
there isn't really a "split",  but you can replicate it with SUBSTR  or SUBSTRING depending on the specific platform.

The reason you can't simply "split" is because SQL requires fixed output at parse time,  if you split a single column into multiple columns the number of columns could vary per row which isn't legal.
0
 
LVL 40

Assisted Solution

by:Kyle Abrahams
Kyle Abrahams earned 250 total points
ID: 33510471
select * from dbo.fn_txt_split(my_field, ',')
/****** Object:  UserDefinedFunction [dbo].[fn_Txt_Split]    Script Date: 08/24/2010 09:10:21 ******/

IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[fn_Txt_Split]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))

DROP FUNCTION [dbo].[fn_Txt_Split]

GO



/****** Object:  UserDefinedFunction [dbo].[fn_Txt_Split]    Script Date: 08/24/2010 09:10:21 ******/

SET ANSI_NULLS ON

GO



SET QUOTED_IDENTIFIER ON

GO





Create Function [dbo].[fn_Txt_Split]( 

    @sInputList varchar(8000) -- List of delimited items 

  , @Delimiter char(1) = ',' -- delimiter that separates items 

) 

RETURNS @list table (Item varchar(8000)) 

as begin 

DECLARE @Item Varchar(8000) 

  

  



WHILE CHARINDEX(@Delimiter,@sInputList,0) <> 0 

BEGIN 

SELECT 

@Item=RTRIM(LTRIM(SUBSTRING(@sInputList,1,CHARINDEX(@Delimiter,@sInputList,0 

)-1))), 

@sInputList=RTRIM(LTRIM(SUBSTRING(@sInputList,CHARINDEX(@Delimiter,@sInputList,0)+1,LEN(@sInputList)))) 

  

IF LEN(@Item) > 0 

INSERT INTO @List SELECT @Item 

  

END 



  

IF LEN(@sInputList) > 0 

INSERT INTO @List SELECT @sInputList -- Put the last item in 

  

return 

END 



GO

Open in new window

0
 
LVL 40

Expert Comment

by:Kyle Abrahams
ID: 33510501
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

As they say in love and is true in SQL: you can sum some Data some of the time, but you can't always aggregate all Data all the time! Introduction: By the end of this Article it is my intention to bring the meaning and value of the above quote to…
This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
This video discusses moving either the default database or any database to a new volume.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now