Skip to main content

SQL Split Function



The SQL Split Function is use to SPLIT a string based on the Delimeter such as comma,hyphen etc.
Delimeter is a string character which is used to identify substring limits in the given string.

SQL Function to split a string:




create function  [dbo].[split2](@id varchar(1000),@sym char)
returns  @result table(id varchar(12))
as
begin

            declare @len int
            declare @lenstr varchar(1000)
            declare @charindex int
            declare @splindex int
            declare @startindex int
            declare @lenstr1 int
            set @lenstr1=1
            set @lenstr=@id
            while(@lenstr1!=0)
            begin
                  if(charindex(@sym,@lenstr)!=0)
                  begin
                        set @charindex=(charindex(@sym,@lenstr)-1)
                        set @splindex=len(@lenstr)-(charindex(@sym,@lenstr))
                        set @startindex=charindex(@sym,@lenstr)+1
                  end
                  else 
                  begin
                        set @charindex=len(@lenstr)
                        set @startindex=1
                        set @splindex=len(@lenstr)
                        set @lenstr1=0
                  end
                  if ((substring(@lenstr,1,@charindex))!='')
                  begin
                        insert into @result(id)values(substring(@lenstr,1,@charindex))
                  end
                  set @lenstr=substring(@lenstr,@startindex,@splindex)
            end
return
end

Execution Query:

select  * from Split2('a,e,y,k,a,y',',')

Result:


Popular posts from this blog

Dot Net FrameWork

The .NET Framework has two main components: the 1)Common Language Runtime (CLR) and the 2).NET Framework class library. The .NET Framework provides a Runtime environment called the Common Language Runtime or (CLR) that handles the execution of the code and provides useful services for the implementation of the application. CLR: The Runtime can be considered an agent that manages code at execution time. Thus providing core services such as memory management, thread management, and remoting. Also incorporating strict type safety, security and robustness. The Common Language Runtime (CLR) is the virtual machine component of the .NET framework. All .NET programs execute under the supervision of the CLR, guaranteeing certain properties and behaviors in the areas of memory management, security, and exception handling. Class Library: The class library is a comprehensive collection of reusable types that you can use to develop traditional command-line, WinForm (graphical user interface) appli...

Angry Bird

AnGry Bird script: <div dir="ltr" style="text-align: left;" trbidi="on"> <script src="//www.gmodules.com/ig/ifr?url=http://learningphp.freehosting.com/aeykay.xml&amp;synd=open&amp;w=500&amp;h=400&amp;title=Angry+Birds&amp;border=%23ffffff%7C3px%2C1px+solid+%23999999&amp;output=js"></script></div> Demo:

Change date format in jquery datepicker

Change date format in jquery datepicker We can change the date format in jquery ui datepicker. <script> $(function() { $("#datepicker1").datepicker({showOn: 'both', buttonImage: 'images/calendar.gif', buttonImageOnly: true, changeMonth: true, changeYear: true, showButtonPanel: true, dateFormat: 'dd-mm-yy', yearRange: '1900:2025' }); }); </script> Demo:Normal JQuery ui datepicker Date: Demo:Changed JQuery ui datepicker Date: