Ver Mensaje Individual
  #7 (permalink)  
Antiguo 09/10/2014, 10:34
Avatar de Libras
Libras
Colaborador
 
Fecha de Ingreso: agosto-2006
Ubicación: En la hermosa perla de occidente
Mensajes: 7.412
Antigüedad: 18 años, 3 meses
Puntos: 774
Respuesta: Separar cadena de caracteres con Store procedure

Código SQL:
Ver original
  1. /****** Object:  UserDefinedFunction [dbo].[SplitApplication]    Script Date: 10/19/2010 15:12:35 ******/
  2. SET ANSI_NULLS ON
  3. GO
  4.  
  5. SET QUOTED_IDENTIFIER ON
  6. GO
  7.  
  8. ALTER FUNCTION [dbo].[SplitApplication](@applicationid VARCHAR(MAX))
  9.  
  10. RETURNS @RtnValue TABLE
  11. (
  12. Id INT IDENTITY(1,1),
  13. DATA nvarchar(MAX)
  14. )
  15. AS
  16. BEGIN
  17. DECLARE @Cnt INT, @SplitOn nvarchar(20)
  18. --SET @SplitOn = '##Receive;'
  19. SET @SplitOn = ';'
  20. SET @Cnt = 1
  21.  
  22. While (Charindex(@SplitOn,@applicationid)>0)
  23. BEGIN
  24. INSERT INTO @RtnValue (DATA)
  25. SELECT
  26. DATA = ltrim(rtrim(SUBSTRING(@applicationid,1,Charindex(@SplitOn,@applicationid)-(len(@SplitOn)))))
  27.  
  28. SET @applicationid = SUBSTRING(@applicationid,Charindex(@SplitOn,@applicationid)+(len(@SplitOn)),len(@applicationid))
  29. SET @Cnt = @Cnt + 1
  30. END
  31.  
  32. INSERT INTO @RtnValue (DATA)
  33. SELECT DATA = ltrim(rtrim(@applicationid))
  34.  
  35. RETURN
  36. END
  37.  
  38. GO
  39.  
  40.  
  41. how TO USE :
  42.  
  43.  
  44. SELECT
  45. CASE WHEN charindex('##',DATA)>=1 THEN SUBSTRING([DATA],1,charindex('##',DATA)-1) ELSE DATA END AS dato FROM
  46. SplitApplication('[email protected]##Receive;vlizarralde@argos .com.co##Receive;[email protected]##Receive;ja [email protected]##Receive;[email protected]##Receive;[email protected]##Receive Deliver;[email protected]##Receive, Deliver;[email protected]')

Resultado final:

[email protected]
vlizarralde@argos .com.co
[email protected]
ja [email protected]
[email protected]
[email protected]
[email protected]
[email protected]


saludos!
__________________
What does an execution plan say to t-sql query? Go f**k yourself, if you are not happy with me

Última edición por Libras; 09/10/2014 a las 10:39