从字符串sql中提取数字

时间:2017-05-30 11:47:26

标签: sql sql-server

我有这个SQL数据:

AT_SendMail_v1

AT_Certificate_v10

AT_Certificate_v100

我想要这个输出:

AT_SendMail_v2

AT_Certificate_v11

AT_Certificate_v101

我有这个代码,但它不适用于超过19的数字:

SELECT CASE 
        WHEN ISNUMERIC(RIGHT([internalname],1)) = 1 
        THEN LEFT([internalname],LEN([internalname])-1)  + cast((CONVERT(INT, RIGHT([internalname],1)) + 1) as varchar(100)) 
        ELSE [internalname] + '_v1'
        END
       ,[id]
FROM [task] 

谢谢:)

6 个答案:

答案 0 :(得分:1)

假设数字始终是字符串的结尾:

使用patindex()

select 
    name
  , new_name = case when patindex('%[_]v[1234567890]%',name) > 0
      then left(name,(patindex('%[_]v[1234567890]%',name)+1))
         + convert(varchar(10),convert(int
           ,right(name,len(name)-(patindex('%[_]v[1234567890]%',name)+1)))
           +1)
       else name+'_v1'
    end
from task

rextester演示:http://rextester.com/AWPXB86451

返回:

+----------------------+----------------------+
|         name         |       new_name       |
+----------------------+----------------------+
| AT_SendMail_v1       | AT_SendMail_v2       |
| AT_Certificate_v10   | AT_Certificate_v11   |
| AT_Certificate_v_v50 | AT_Certificate_v_v51 |
+----------------------+----------------------+

答案 1 :(得分:0)

你可以使用SUBSTRING来获取_v之后的数字,之后检查它是否是使用ISNUMERIC的数字,之后它将该数字增加1,将其转换为字符串并连接字符串

DECLARE @internalname NVARCHAR(40)
SET @internalname = 'AT_SendMail_v19'

SELECT 
    CASE WHEN ISNUMERIC(SUBSTRING(@internalname, CHARINDEX('_v', @internalname) + 2, LEN(@internalname))) = 1
         THEN SUBSTRING(@internalname,+2, CHARINDEX('_v',@internalname)) + CAST(SUBSTRING(@internalname, CHARINDEX('_v', @internalname) + 2, LEN(@internalname)) + 1 AS NVARCHAR(60))
         ELSE @internalname + '_v1'
    END

答案 2 :(得分:0)

这是增加数字的一种方法,然后你可以把它建成你的字符串......

DECLARE @str NVARCHAR(20) = 'AT_SendMail_v1';

DECLARE @intCharIndex INT = CHARINDEX('_v',@str);

IF(@intCharIndex = 0)

BEGIN 

    SELECT 'No string found';
END

IF(@intCharIndex > 0)

BEGIN 
    DECLARE @int INT = LEN(@str) - @intCharIndex - 1;

    DECLARE @intVersion INT = RIGHT(@str,@int) + 1;

    SELECT LEFT(@str,@intCharIndex) + 'v' + RTRIM(CAST(@intVersion AS NVARCHAR(4)));
END

答案 3 :(得分:0)

试试这个:

declare @test varchar(50) = 'AT_SendMail_v1';

select Output = case when ISNUMERIC( substring ( @test , charindex('_v',@test)+2 , len(@test)) )=1 then

         left (@test , charindex('_v',@test)+1) + cast( cast(substring ( @test , charindex('_v',@test)+2 , len(@test)) as int)+1 as varchar )
         else
         @test+'_v1' end;

Demo

答案 4 :(得分:0)

这对我来说是最好的代码,谢谢:)

SELECT 
    CASE 
        WHEN CHARINDEX('_v', [internalname], 1) <> 0 THEN LEFT([internalname], CHARINDEX('_v', [internalname], 1) + 1) + CAST(CAST(RIGHT([internalname], LEN([internalname]) - CHARINDEX('_v', [internalname], 1) - 1) AS INT) + 1 AS VARCHAR(100)) 
        ELSE [internalname] + '_v1' 
    END 
    ,[id] 
FROM [task]

答案 5 :(得分:-1)

在_v

之后如何满足任何数字
Select Left(internalName, Len(internalName) - CharIndex('v_', Reverse(internalName)) + 1) + Cast((Cast((Right(internalName, CharIndex('v_', Reverse(internalName)) -1)) As Integer) + 1) As NVarchar(32))
From task