Do a SQL Server procedure receiving string with special character and obtaining only the numbers

2

Hello, I have a system developed in Classic ASP, in which I select up to 5 times on a screen. These times are sent to the other page via querystring, however the next format is followed by an exclamation. Example 7, 10.19. What I need is to get these codes (7, 10, and 19) and use them to insert a record into a SQL Server table with each one.

I'm trying to do this via procedure, because my ASP skills are soooo small.

Could someone give me a light?

    
asked by anonymous 27.07.2015 / 17:56

1 answer

0

First create a function to divide your string into separate values:

CREATE FUNCTION dbo.splitstring ( @separador CHAR, @stringToSplit VARCHAR(MAX) )
  RETURNS
  @returnList TABLE ([Name] [nvarchar] (500))
AS
BEGIN

  DECLARE @name NVARCHAR(255)
  DECLARE @pos INT

  WHILE CHARINDEX(@separador, @stringToSplit) > 0
    BEGIN
    SELECT @pos  = CHARINDEX(@separador, @stringToSplit)  
    SELECT @name = SUBSTRING(@stringToSplit, 1, @pos-1)

    INSERT INTO @returnList 
    SELECT @name

    SELECT @stringToSplit = SUBSTRING(@stringToSplit, @pos+1, LEN(@stringToSplit)-@pos)
 END

 INSERT INTO @returnList
 SELECT @stringToSplit

 RETURN
END

Call the created function by passing a string containing the values separated by "!":

SELECT * FROM dbo.splitstring('!', '91!12!65!78!56!789')

The result for this query will be as below:

Toinsertthevaluesinyourothertable,simplyadaptthefollowingcommandaccordingtothecolumnsyouhaveinyourtable:

INSERTINTOTabelaDeDestinoSELECTNameFROMdbo.splitstring('!','91!12!65!78!56!789')

ThisisanadaptationoftheEnglishresponse link     

27.07.2015 / 18:08