2017-02-22 52 views
-1

我想創建一個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

回答

0

我錯過了年底完成功能

0

創建函數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