| 1 |
|
|---|
| 2 | SET ANSI_NULLS ON
|
|---|
| 3 | GO
|
|---|
| 4 | SET QUOTED_IDENTIFIER ON
|
|---|
| 5 | GO
|
|---|
| 6 |
|
|---|
| 7 | --================================================
|
|---|
| 8 | -- Create Inline Table-valued Function template
|
|---|
| 9 | --================================================
|
|---|
| 10 |
|
|---|
| 11 | ALTER FUNCTION [dbo].[SplitAsScalar]
|
|---|
| 12 | (@in_del_field VARCHAR(4000),
|
|---|
| 13 | @in_pos INT,
|
|---|
| 14 | @in_del VARCHAR(4000) = '/')
|
|---|
| 15 | RETURNS VARCHAR(4000)
|
|---|
| 16 | AS
|
|---|
| 17 | BEGIN
|
|---|
| 18 | /**
|
|---|
| 19 | * <hr>
|
|---|
| 20 | * SPLIT Scalar User-Defined function JG 1/13/2006
|
|---|
| 21 | * <hr>
|
|---|
| 22 | * The split function will take a delimted string value
|
|---|
| 23 | * and return a value from a location in the string.
|
|---|
| 24 | * <PRE>
|
|---|
| 25 | * FOR EXAMPLE...
|
|---|
| 26 | *
|
|---|
| 27 | * print dbo.split('franklin,delano,roosevelt',1,',')
|
|---|
| 28 | * print dbo.split('franklin,delano,roosevelt',2,',')
|
|---|
| 29 | * print dbo.split('franklin,delano,roosevelt',3,',')
|
|---|
| 30 | * GO
|
|---|
| 31 | *
|
|---|
| 32 | * THIS RETURNS...
|
|---|
| 33 | *
|
|---|
| 34 | * franklin
|
|---|
| 35 | * delano
|
|---|
| 36 | * roosevelt
|
|---|
| 37 | *
|
|---|
| 38 | * </PRE>
|
|---|
| 39 | * @param @in_del_field Delimited Text string passed to the function
|
|---|
| 40 | * @param @in_pos Position of value to return
|
|---|
| 41 | * @param @in_del Delimiter. DEFAULT value is ","
|
|---|
| 42 | * @headcom
|
|---|
| 43 | */
|
|---|
| 44 | DECLARE @retVal VARCHAR(4000)
|
|---|
| 45 | DECLARE @varFldDel VARCHAR(4000) SET @varFldDel = @in_del_field + @in_del
|
|---|
| 46 | DECLARE @i int SET @i = 0
|
|---|
| 47 | DECLARE @varExit VARCHAR(5) SET @varExit = 'FALSE'
|
|---|
| 48 | DECLARE @intLastLen int SET @intLastLen = 1
|
|---|
| 49 | --
|
|---|
| 50 | IF @in_pos < 1
|
|---|
| 51 | SET @retVal = 'Not Valid Option, 1 is the first occurance of an item'
|
|---|
| 52 | ElSE
|
|---|
| 53 | --> loop through till the correct column is found
|
|---|
| 54 | WHILE @varExit = 'FALSE'
|
|---|
| 55 | BEGIN
|
|---|
| 56 | SET @i = @i + 1
|
|---|
| 57 | If @i > 1
|
|---|
| 58 | BEGIN
|
|---|
| 59 | SET @varFldDel = SUBSTRING(@varFldDel,@intLastLen,8000)
|
|---|
| 60 | END
|
|---|
| 61 | If CHARINDEX(@in_del,@varFldDel) < 1
|
|---|
| 62 | BEGIN
|
|---|
| 63 | RETURN NULL
|
|---|
| 64 | END
|
|---|
| 65 | --
|
|---|
| 66 | SET @retVal = SUBSTRING(@varFldDel,1,(CHARINDEX(@in_del,@varFldDel)-1))
|
|---|
| 67 | SET @intLastLen = LEN(@retVal) + 2
|
|---|
| 68 | If @i = @in_pos
|
|---|
| 69 | BREAK
|
|---|
| 70 | END
|
|---|
| 71 | --
|
|---|
| 72 | RETURN @retVal
|
|---|
| 73 | --
|
|---|
| 74 | END
|
|---|
| 75 | GO
|
|---|
| 76 |
|
|---|
| 77 | SET ANSI_NULLS OFF
|
|---|
| 78 | GO
|
|---|
| 79 | SET QUOTED_IDENTIFIER OFF
|
|---|
| 80 | GO
|
|---|
| 81 |
|
|---|