![](/img/trans.png)
[英]How to get the Starting date and ending date of weeks using week number in my case using SQL Server?
[英]SQL How to get the ending number
我正在使用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
在大多数情况下,它的使用有所帮助。
但不是全部。
字符串如下所示:
string-goes-here-123450
string-2-goes-here-1233
在第一种情况下,正确的输出是123450,但是在第二种情况下,输出是21233。
我的问题是我如何才能得到结尾数字的最后一位,该结尾数字与“-”分开
试试这个...先找到最后一次出现-
然后取字符串的结尾
SET @pos = LEN(@string) - CHARINDEX('-',REVERSE(@string))
select substr(@string, @pos+1, @len(@str))
编辑:它可能是@pos-1
...我在这台计算机上没有sql server,所以我无法测试,mysql的功能也不同,以至于我不确定我是否正确翻译了它。 请尝试一下,让我知道。
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
生产: -
input output
string-goes-here-123450 123450
string-2-goes-here-1233 1233
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.