-5

Need to pull the numeric values from the columns Routing number and Account_Number

The values in the Routing_number column:

031100225  TOTAL FRAUD,
101190178  NON CASH,
071000288 NOT ON US,
101104407  10 DAY SENT,
065000090    REQ 10 DAY 81615,
101101141, DATED 032409,
101100045, FOR A FORD TAURUS

I need to select Routing number as - 031100225,101190178,071000288

The values in the Account_number column:

2079950059473 TOTAL FRAUD,
000480152617 KC MASTER COMP,
2538867, STALE DATE, CHECK
7003475514   FROZEN BLOCKED,
5049530455, NSF CHECK

I need to select account number as 2079950059473,000480152617

I need a SUBSTRING function to select only the numeric part within the column.

Thanks

kris
  • 39
  • 1
  • 1
  • 5
  • are the routing numbers always 9 digits? are the account numbers always a certain number of digits? – S3S Oct 21 '16 at 14:52
  • 1
    Use CHARINDEX to look for the first space, embedded into the Left function. – SS_DBA Oct 21 '16 at 14:54
  • the routing numbers are nine digits and there is no limit for the account numbers. –  kris Oct 21 '16 at 14:56
  • some of the values are even have the "," and "-" symbols to. So I need to pick the number before the space or any wierd symbol like "," "space" "." etc –  kris Oct 21 '16 at 14:58
  • Possible duplicate of [SQL take just the numeric values from a varchar](http://stackoverflow.com/questions/11333078/sql-take-just-the-numeric-values-from-a-varchar). As far as generating a comma seperated list, use For XML path to combine rows in SQL Server. you've not provided enough information to know which rows to combine (how they combine) so all we can do is provide an example. Example [here though](http://stackoverflow.com/questions/194852/concatenate-many-rows-into-a-single-text-string). – xQbert Oct 21 '16 at 15:02
  • http://stackoverflow.com/help/mcve – S3S Oct 21 '16 at 15:35

3 Answers3

2

Use PATINDEX

SELECT LEFT('031100225  TOTAL FRAUD',PATINDEX('%[^0-9]%','031100225  TOTAL FRAUD') - 1)

SELECT LEFT('2079950059473 TOTAL FRAUD,',PATINDEX('%[^0-9]%','2079950059473 TOTAL FRAUD,') - 1)

Etc...

SELECT 
    LEFT(Routing_number,PATINDEX('%[^0-9]%',Routing_number) - 1) as ROUT,
    LEFT(Account_number,PATINDEX('%[^0-9]%',Account_number) - 1) as ACCT
FROM 
    YourTable

EDIT

SELECT
        CASE
            WHEN Routing_number IS NULL THEN NULL
            WHEN TRY_CONVERT(INT,Routing_number) IS NULL THEN LEFT(Routing_number,PATINDEX('%[^0-9]%',Routing_number) - 1)
            ELSE Routing_number 
        END AS ROUT,
        CASE
            WHEN Account_number IS NULL THEN NULL
            WHEN TRY_CONVERT(INT,Account_number) IS NULL THEN LEFT(Account_number,PATINDEX('%[^0-9]%',Account_number) - 1)
            ELSE  Account_number
        END AS ACCT
    FROM 
        YourTable

Using ISNUMERIC instead of TRY_CONVERT

SELECT
        CASE
            WHEN Routing_number IS NULL THEN NULL
            WHEN ISNUMERIC(Routing_number) = 0 THEN LEFT(Routing_number,PATINDEX('%[^0-9]%',Routing_number) - 1)
            ELSE Routing_number 
        END AS ROUT,
        CASE
            WHEN Account_number IS NULL THEN NULL
            WHEN ISNUMERIC(Account_number) = 0 THEN LEFT(Account_number,PATINDEX('%[^0-9]%',Account_number) - 1)
            ELSE  Account_number
        END AS ACCT
    FROM 
        YourTable
S3S
  • 24,809
  • 5
  • 26
  • 45
  • i tried it and its throwing this error. –  kris Oct 21 '16 at 15:06
  • What error... @kris – S3S Oct 21 '16 at 15:07
  • Invalid length parameter passed to the LEFT or SUBSTRING function. –  kris Oct 21 '16 at 15:11
  • @kris then you are entering something incorrectly. If you copy those top two lines and run them it will not throw an error. If you are getting an error when running it against your dataset then you must have a syntax issue. Be sure you copy them exactly. – S3S Oct 21 '16 at 15:13
  • I havent chnaged anything in the two lines.. I juz added my table name in place of "yourTable" dats it –  kris Oct 21 '16 at 15:15
  • 1
    Can you post more eaxmples of your data in the original question. – S3S Oct 21 '16 at 15:16
  • Yeah sure i wiill!! –  kris Oct 21 '16 at 15:18
  • @kris try that edit i just posted. You must have some values which don't have a space thus `-1` would be passed to the `LEFT` function causing the error. – S3S Oct 21 '16 at 15:26
  • I tried it and its throwing me an error 'TRY_CONVERT' is not a recognized built-in function name. –  kris Oct 21 '16 at 15:31
  • @kris you tagged the OP with 2012... are you not on SQL 2012? – S3S Oct 21 '16 at 15:31
  • TRY_CONVERT is only available with 2012. I added an ISNUMERIC option thought his isn't the best option, it should work for your odd data @kris – S3S Oct 21 '16 at 15:34
  • I am on Sql Server 2014 and 64 bit –  kris Oct 21 '16 at 15:35
  • If that DB is 2014 then it should work. https://msdn.microsoft.com/en-us/library/hh230993.aspx Are you sure you aren't just running SSMS 2014 and querying a DB that is older than 2012? I think this is the case, otherwise you wouldn't get that error. Either way, the solutions above will work. Cheers. – S3S Oct 21 '16 at 15:36
  • ..i tried the ISNUMERIC as well it is throwing the same error "Invalid length parameter passed to the LEFT or SUBSTRING function." –  kris Oct 21 '16 at 15:38
  • Then you have data in that field that isn't like your example. Read this stackoverflow.com/help/mcve – S3S Oct 21 '16 at 15:39
  • I checked the version using " SELECT @@VERSION" and this gave me Microsoft SQL Server 2014 - 12.0.4213.0 (X64) Jun 9 2015 12:06:16 Copyright (c) Microsoft Corporation Standard Edition (64-bit) on Windows NT 6.1 (Build 7601: Service Pack 1) (Hypervisor) –  kris Oct 21 '16 at 15:39
  • Is that because the columns even had the null values in them? –  kris Oct 21 '16 at 15:41
  • No i check to see if it's null first. so if it's null the case statement exits returning NULL. – S3S Oct 21 '16 at 15:47
0

I've created a function to perform this operation in a database I use, keeps the query syntax more straight forward:

CREATE FUNCTION [dbo].[RemoveNonNumericCharacters](@result VARCHAR(1000))
RETURNS VARCHAR(1000)
AS
BEGIN

    DECLARE @Numbers AS VARCHAR(50) = '%[^0-9]%'
    WHILE PatIndex(@Numbers , @result) > 0
        SET @Temp = Stuff(@result, PatIndex(@Numbers , @result), 1, '')

    RETURN @result
END

Sample:

SELECT dbo.RemoveNonNumericCharacters('20sdsdsd sd sdsd3920keosd e20e920e')

Returns:

20392020920

To use in a SELECT:

SELECT dbo.RemoveNonNumericCharacters(Routing_number) AS NewValue
FROM [YOUR_TABLE]
Tanner
  • 22,205
  • 9
  • 65
  • 83
  • For an even faster function that removes non-numeric characters have a look at DigitsOnlyEE located here: http://www.sqlservercentral.com/scripts/String+Function/141686/ We did a lot of tests and had a lengthy discussion about the most efficient way to do this a few years back, see this SSC thread: http://www.sqlservercentral.com/Forums/Topic1585850-391-2.aspx – Alan Burstein Oct 22 '16 at 03:53
0

scsimon's nicely designed PATINDEX solution is the way to go but it will throw that Invalid length parameter passed to the LEFT or SUBSTRING function error you mentioned if you pass an empty string or a record which only contains numbers. Below is a similar solution that handles purely numeric values and empty strings:

DECLARE @yourTable TABLE (Routing_Number varchar(100));
INSERT @yourTable VALUES 
('031100225 TOTAL FRAUD'),('101190178 NON CASH'),('071000288'),('');

-- This will fail when there's only numbers or the string is empty:
/*
SELECT 
  SUBSTRING
  (
    Routing_Number, 1,
    PATINDEX('%[^0-9]%',Routing_number)-1
  ) AS ROUT
FROM @yourTable;
*/

SELECT 
  SUBSTRING
  (
    Routing_Number, 1,
    ISNULL(NULLIF(PATINDEX('%[^0-9]%',Routing_number)-1,-1),8000)
  ) AS ROUT
FROM @yourTable; 
Alan Burstein
  • 7,770
  • 1
  • 15
  • 18