[英]SQL How to get the ending number
I was using follow function to get numbers from a string. 我正在使用Follow函数从字符串中获取数字。
CREATE FUNCTION dbo.udf_GetNumeric
(@strAlphaNumeric VARCHAR(256))
RETURNS VARCHAR(256)
AS
BEGIN
DECLARE @intAlpha INT
SET @intAlpha = PATINDEX('%[^0-9]%', @strAlphaNumeric)
BEGIN
WHILE @intAlpha > 0
BEGIN
SET @strAlphaNumeric = STUFF(@strAlphaNumeric, @intAlpha, 1, '' )
SET @intAlpha = PATINDEX('%[^0-9]%', @strAlphaNumeric )
END
END
RETURN ISNULL(@strAlphaNumeric,0)
END
GO
And the usage of it helped in most cases. 在大多数情况下,它的使用有所帮助。
But not in all. 但不是全部。
Strings looks like this: 字符串如下所示:
string-goes-here-123450
string-2-goes-here-1233
In the first case, output was correct which is 123450, but in second, the output was 21233. 在第一种情况下,正确的输出是123450,但是在第二种情况下,输出是21233。
My question is how can i just get the last bit of ending number which separates from "-" 我的问题是我如何才能得到结尾数字的最后一位,该结尾数字与“-”分开
try this... first find the last occurance of -
and then take the end of the string 试试这个...先找到最后一次出现
-
然后取字符串的结尾
SET @pos = LEN(@string) - CHARINDEX('-',REVERSE(@string))
select substr(@string, @pos+1, @len(@str))
EDIT: it might be @pos-1
... I don't have sql server on this computer so I can't test and the functions for mysql are different enough that I'm not sure I translated it correctly. 编辑:它可能是
@pos-1
...我在这台计算机上没有sql server,所以我无法测试,mysql的功能也不同,以至于我不确定我是否正确翻译了它。 Please try this out and let me know. 请尝试一下,让我知道。
create table #test(
input varchar(50)
)
go
insert into #test values
('string-goes-here-123450'),
('string-2-goes-here-1233')
go
select t.input, right(t.input,charindex('-',reverse(t.input))-1) as output
from #test t
produces:- 生产: -
input output
string-goes-here-123450 123450
string-2-goes-here-1233 1233
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.