子串函数

时间:2016-10-21 14:35:43

标签: sql-server tsql sql-server-2012 substring

需要从列Routing numberAccount_Number

中提取数值

Routing_number列中的值:

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

我需要选择路由号码为 - 031100225,101190178,071000288

Account_number列中的值:

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

我需要选择帐号为2079950059473,000480152617

我需要一个SUBSTRING函数来仅选择列中的数字部分。

由于

3 个答案:

答案 0 :(得分:2)

使用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)

等等...

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

修改

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

使用ISNUMERIC代替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

答案 1 :(得分:0)

我已经创建了一个函数来在我使用的数据库中执行此操作,使查询语法更加直接:

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

样品:

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

返回:

20392020920

SELECT中使用:

SELECT dbo.RemoveNonNumericCharacters(Routing_number) AS NewValue
FROM [YOUR_TABLE]

答案 2 :(得分:0)

scsimon精心设计的PATINDEX解决方案是可行的方法,但是如果你传递一个空字符串或只包含一个记录,它会抛出传递给你提到的LEFT或SUBSTRING函数错误的无效长度参数数字。下面是一个处理纯数值和空字符串的类似解决方案:

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;