2017-02-22 5 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 を開始します:firstName @fn varchar(50)を宣言します。 - インデックス変数を宣言して、姓と名を分離する区切り文字のインデックスを見つけます。 @index intを宣言します。 - セパレータのインデックス値を取得する SET @index = CHARINDEX( '、'、@ fullname); --checkデフォルトの区切りは(、)が存在する場合 IF @Index> 0 それは、インデックス+ @ 2、 は= SUBSTRING(@fullnameが@fn設定し をBEGINまず名前を見つけるために、SUBSTRING関数を使用してい--if 、LEN(@fullname) - @ index); 終了 返信@fn 終了