SQL函数调试

问题描述:

我想创建一个sql函数,它将返回全名列的名字。现在它以Last,First的格式返回姓氏。这是假设返回后的名字,SQL函数调试

我的子字符串函数是假设开始抓住@ index + 2的信息,但它是从头开始。我不确定什么是错的?

Create Function dbo.fnGetFirstName (@fullname varchar(100)) 
Returns Varchar(50) 
AS 
Begin 
--declare the local variable: firstName 
Declare @fn varchar(50); 
--declare the index variable to find the index of the separator that separates last name from first name 
Declare @index int; 
--get the separator index value 
SET @index = CHARINDEX(',',@fullname); 
--check if the default separator (,) exists 
IF @index > 0 
--if it does, use the substring function to find the First name 
BEGIN 
    Set @fn = SUBSTRING(@fullname, @index+2, LEN(@fullname)[email protected]); 

END 
+0

它似乎有一个缺失END – McNets

+0

这是一个失踪的结局。谢谢你的提示。不能相信我错过了..... –

+0

我认为它工作正常:http://rextester.com/ISBH13610 – McNets

我错过了年底完成功能

创建函数dbo.fnGetFirstName(@fullname VARCHAR(100)) 返回VARCHAR(50) AS 开始 --declare局部变量:名字 声明@fn varchar(50); - 声明索引变量以查找将姓氏从名字中分离出来的分隔符的索引 Declare @index int; - 获取分隔符索引值 SET @index = CHARINDEX(',',@ fullname); --check如果默认分离器(,)存在 IF @index> 0 --IF它,使用子串函数来查找名字 BEGIN 设置@fn = SUBSTRING(@fullname,@指数+ 2 ,LEN(@fullname) - @ index); END return @fn END