sql server 2005 - SQL function, extract numbers before - -


I am playing with the creation of a SQL function which will draw a number from a title, which is the following code below. However, I want to modify this function to parse numbers in sections. For example:

Current data in Title field:

 QW 1 RT 30 9 23-1 QW 1 RT 29-1 QW 1 RT 750-1 QW RT 750-1 < Tables have been created after the function, the title field is worked on: 

 column 1 column 2 column 3 column 4 1 30 9 23 1 1 2 9 1 tap 1 750 1 tap faucet 750 1 tap 
announcement as announced @NonNumericIndex set int @NonNumericIndex = PATINDEX ('% [^ 0- 9]%', @ number ) WHILE @NonNumericIndex & gt; 0 set start @Numbers = REPLACE (@ number, SUBSTRING (@ number, @NonNumericIndex, 1), set) @NonNumericIndex = PATINDEX ('% [^ 0-9]%', @ number) Numbers END

This is a way

though in fact the end I realized that the format was originally fixed more than it was realized so that you could better calculate the columns using different string manipulation functions.

 with  select 'QW 1 RT 29-1' Union All Select 'QW 1 RT 30 9-23-1' AS Title Union Select All as TestTable ' QW 1 RT 750-1 'Select All Union' QW RT 750- 1 ') Select Title [1] AS [Column 1], [2] AS [Column 2], [3] AS [Column 3] , [4] Apply TestStar CRS from AS [Column 4] DBO. ) The following TVF uses  

in PIVOT (max (number) IDX ([1], [2], [3], [4]);

  Create Function GetNumbers (@Numbers NVARCHAR (2000)) @Results table (IDX returns INT identification (returns 1,1), declared as number INT, @NonNumericIndex INT, @NumericIndex INT set @NumericIndex = PATINDEX ('% [0-9]%' start, number @) IF (@NumericIndex & gt; 4) --First column there is no @Results insert value (NULL) WHILE @ Numeric Index; Numbers = Rights (@ number, LEN (@Numbers) - @NumericIndex +1) Set @NonNumericIndex = PATINDEX ('% [^ 0-9]%', @ number I) (@NonNumericIndex-1) set IF (@NonNumericIndex = 0) INSERT INTO @Results Values ​​(@Numbers) @Results values ​​and insert the return end (@ number, @NonNumericIndex -1) set @Numbers = rights (number, LEN (@Numbers) - @ NonNumericIndex +1) Set @NumericIndex = PATINDEX ('% [0-9]%', @ number) End return end  

Comments