Funções sempre são úteis no nosso dia-a-dia, não é mesmo? Por isto criei uma função que recebe uma string, um delimitador e retorna uma lista de string dividas por aquele delimitador
Vejam só que bacana:
CREATE FUNCTION [dbo].[FnSplit]
(
@List nvarchar(2000),
@SplitOn nvarchar(5)
)
RETURNS @RtnValue table
(
Id int identity(1,1),
Value nvarchar(100)
)
AS
BEGIN
While (Charindex(@SplitOn,@List)>0)
Begin
Insert Into @RtnValue (value)
Select
Value = ltrim(rtrim(Substring(@List,1,Charindex(@SplitOn,@List)-1)))
Set @List = Substring(@List,Charindex(@SplitOn,@List)+len(@SplitOn),len(@List))
End
Insert Into @RtnValue (Value)
Select Value = ltrim(rtrim(@List))
Return
END
--como utilizar select * from dbo.[FnSplit]('ab;cd',';')
--retona ab e cd em campos separados
O retorno seria:
Id Value
1 ab
2 cd
Por hoje é só! Um grande abraço a todos!
