-1

我想编写一个使用字符串值和起始索引号和计数的 UDF:

GetValueUDF("i am 18 years old.",6,2) output: 18 , 6: .nth 起点, 2: count

GetValueUDF("我 101 岁。",6,3) 输出:101

GetValueUDF("我 101 岁。",1,4) 输出:"我是"

4

1 回答 1

3

我不太确定你为什么要为此创建一个用户定义的函数,因为这就是substring函数的作用。但是您应该能够使用以下内容:

create function GetValueUDF
(
  @string varchar(max), 
  @startPosition int,
  @length int
)
returns varchar(max)
as

begin
  return substring(@string, @startPosition, @length)
end

请参阅带有演示的 SQL Fiddle

于 2012-12-14T11:05:08.263 回答