Skip to content
David Kittell
David Kittell

Application & System: Development / Integration / Orchestration

  • Services
    • Application Development
    • Online Application Integration
  • Code
  • Online Tools
  • Tech Support
David Kittell

Application & System: Development / Integration / Orchestration

Delimiter Split

Posted on September 30, 2013October 26, 2015 By David Kittell
IF OBJECT_ID(N'dbo.fnSplit') IS NOT NULL
    DROP FUNCTION dbo.fnSplit
GO

CREATE FUNCTION dbo.fnSplit(
    @sInputList VARCHAR(8000) -- List of delimited items
  , @sDelimiter VARCHAR(8000) = ',' -- delimiter that separates items
) RETURNS @List TABLE (item VARCHAR(8000))

BEGIN
DECLARE @sItem VARCHAR(8000)
WHILE CHARINDEX(@sDelimiter,@sInputList,0) <> 0
 BEGIN
 SELECT
  @sItem=RTRIM(LTRIM(SUBSTRING(@sInputList,1,CHARINDEX(@sDelimiter,@sInputList,0)-1))),
  @sInputList=RTRIM(LTRIM(SUBSTRING(@sInputList,CHARINDEX(@sDelimiter,@sInputList,0)+LEN(@sDelimiter),LEN(@sInputList))))

 IF LEN(@sItem) > 0
  INSERT INTO @List SELECT @sItem
 END

IF LEN(@sInputList) > 0
 INSERT INTO @List SELECT @sInputList -- Put the last item in
RETURN
END
GO
select * from dbo.fnSplit('1,22,333,444,,5555,666', ',')

Source: http://social.msdn.microsoft.com/Forums/sqlserver/en-US/4126a010-6885-4eb0-b79c-c798c90edb85/how-to-split-comma-delimited-string

Originally Posted on September 30, 2013
Last Updated on October 26, 2015
All information on this site is shared with the intention to help. Before any source code or program is ran on a production (non-development) system it is suggested you test it and fully understand what it is doing not just what it appears it is doing. I accept no responsibility for any damage you may do with this code.

Related

Code MSSQL MSSQL - Functions SQL

Post navigation

Previous post
Next post

Related Posts

Ektron Rename Templates

Posted on October 23, 2013October 26, 2015

This script will rename template paths SET XACT_ABORT ON DECLARE @currval NVARCHAR(500) DECLARE @newval NVARCHAR(500) DECLARE @search NVARCHAR(500) DECLARE @replace NVARCHAR(500) SET @search = ‘/Index.aspx’ –string to find SET @replace = ‘/Default.aspx’ –replacement string DECLARE @pos INT DECLARE @id BIGINT BEGIN TRAN DECLARE curs CURSOR LOCAL FAST_FORWARD FOR SELECT template_id…

Read More

MSSQL Get Domain From Email Address

Posted on October 19, 2018

SELECT DISTINCT RIGHT(Email, LEN(Email) – CHARINDEX(‘@’, email)) Domain FROM contacts SELECT RIGHT(Email, LEN(Email) – CHARINDEX(‘@’, email)) Domain, COUNT(Email) EmailCount FROM contacts WHERE LEN(Email) > 0 GROUP BY RIGHT(Email, LEN(Email) – CHARINDEX(‘@’, email)) ORDER BY EmailCount DESC, Domain Reference: https://blog.sqlauthority.com/2011/06/18/sql-server-selecting-domain-from-email-address/ All information on this site is shared with the intention to…

Read More

PHP – Latitude Latitude to Maidenhead Grid – HAM Radio

Posted on June 14, 2020August 17, 2024

Recently I was asked by a HAM Radio friend to work on a conversion of latitude and longitude coordinates to maidenhead grid. The code below is in “rough” PHP code on purpose to help illustrate the math behind the conversion. Feel free to clean/optimize it all I ask is that…

Read More

Code

Top Posts & Pages

  • PowerShell - Rename Pictures to Image Taken
  • Front Page
  • C# - Start/Stop/Restart Services
  • MacPorts / HomeBrew - Rip CD tracks from terminal
  • PowerShell - Show File Extensions

Recent Posts

  • Javascript – Digital Clock with Style
  • BASH – Web Ping Log
  • BASH – Picture / Video File Name Manipulation
  • Mac OSX Terminal – Create SSH Key
  • Bash – Rename Picture

Top Posts

  • PowerShell - Rename Pictures to Image Taken
  • C# - Start/Stop/Restart Services
  • MacPorts / HomeBrew - Rip CD tracks from terminal
  • PowerShell - Show File Extensions
  • Open On Screen Keyboard (OSK)
  • SQLite - Auto-Increment / Auto Generate GUID
©2025 David Kittell | WordPress Theme by SuperbThemes