SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Removing alpha and leaving numeric
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

InNomina
Starting Member

USA
40 Posts

Posted - 01/24/2013 :  13:24:54  Show Profile  Reply with Quote
I have a column that has mixed alpha numeric<letters and numbers> in no particular format. I need to extract the numbers to another column.

The numbers are in different places< although always togather>

example:
Cell 1: 3 Audit Year Cycles or Current Year + 10 (which ever is longer)
Cell 2: Active + 5
Cell 3: Termination of Plan + 10

So I need only the numbers that are together i.e. in cell 1 the 10, cell 2 the 5 and cell 3 the 10

Is this possible?


-------------------------
"If you never fail, you're not trying hard enough"

InNomina
Starting Member

USA
40 Posts

Posted - 01/24/2013 :  16:25:23  Show Profile  Reply with Quote
Ok, so the solution was to create a function.
-------

CREATE FUNCTION [dbo].[fnRemoveNonNumeric]
(
@BadString nvarchar(20)
)
RETURNS nvarchar(20)
AS
BEGIN

DECLARE @nPos INTEGER
SELECT @nPos = PATINDEX('%[^0-9_]%', @BadString)

WHILE @nPos > 0
BEGIN
SELECT @BadString = STUFF(@BadString, @nPos, 1, '')
SELECT @nPos = PATINDEX('%[^0-9_]%', @BadString)
END

RETURN @BadString
END

To test my function I used...
update test set import = [dbo].[fnRemoveNonNumeric](IMPORT)

Worked like a charm!

Hope this saves a user out there hours of frustration!

-------------------------
"If you never fail, you're not trying hard enough"
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.05 seconds. Powered By: Snitz Forums 2000