我想創建一個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
它似乎有一個缺失END – McNets
這是一個失蹤的結局。謝謝你的提示。不能相信我錯過了..... –
我認爲它工作正常:http://rextester.com/ISBH13610 – McNets