Solved

What is the SQL split command?

Posted on 2010-08-24
3
607 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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In database programming, custom sort order seems to be necessary quite often, at least in my experience and time here at EE. Within the realm of custom sorting is the sorting of numbers and text independently (i.e., treating the numbers as number…
Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…

911 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

25 Experts available now in Live!

Get 1:1 Help Now