Question : Function Design help

Hi All,
I have the following piece of code that I know works fine:

DECLARE @NumStr varchar(1000)
SET @NumStr = 'Seg. Caracas Misc. XL 3rd Layer (1.01: VX - CARGO  ALL RISKS  EXCLUDING WAR ONLY RISKS)'

BEGIN
WHILE PATINDEX('%[^(0-9)]%',@NumStr)> 0
SET @NumStr = REPLACE(@NumStr,SUBSTRING(@NumStr,PATINDEX('%[^(0-9)]%',@NumStr),1),'')
END

PRINT left(substring(@NumStr,charindex('(',@numstr)+1,(charindex(')',@numstr,charindex('(',@numstr)+1)-1)),len(substring(@NumStr,charindex('(',@numstr)+1,(charindex(')',@numstr,charindex('(',@numstr)+1)-1)))-1)

I need to create a function that will allow me to call within a query to ultimatly update a new field in my table with the results.
Not only this I dont want it to do the work on one string only, I need it to do the work on a specified field in my database.

Could someone help me put together a function to do this?

Answer : Function Design help

a function would look like this:

CREATE FUNCTION dbo.ExtractData(@NumStr varchar(1000))
RETURNS VARCHAR(1000)
AS
BEGIN
--SET @NumStr = 'Seg. Caracas Misc. XL 3rd Layer (1.01: VX - CARGO  ALL RISKS  EXCLUDING WAR ONLY RISKS)'

  WHILE PATINDEX('%[^(0-9)]%',@NumStr)> 0
    SET @NumStr = REPLACE(@NumStr,SUBSTRING(@NumStr,PATINDEX('%[^(0-9)]%',@NumStr),1),'')
  SET @numstr = left(substring(@NumStr,charindex('(',@numstr)+1,(charindex(')',@numstr,charindex('(',@numstr)+1)-1)),len(substring(@NumStr,charindex('(',@numstr)+1,(charindex(')',@numstr,charindex('(',@numstr)+1)-1)))-1)
  RETURN @numstr
END


and use it like this:

select dbo.ExtractData('Seg. Caracas Misc. XL 3rd Layer (1.01: VX - CARGO  ALL RISKS  EXCLUDING WAR ONLY RISKS)'

or

select yourfield, dbo.ExtractData(yourfield) from yourtable
Random Solutions  
 
programming4us programming4us