Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用于从字符串中解析/拆分名字和姓氏的T-SQL函数_Sql_Function_Tsql_Parsing_Split - Fatal编程技术网

用于从字符串中解析/拆分名字和姓氏的T-SQL函数

用于从字符串中解析/拆分名字和姓氏的T-SQL函数,sql,function,tsql,parsing,split,Sql,Function,Tsql,Parsing,Split,我可以使用一些帮助来创建一个函数,该函数将解析或拆分字符串的名字作为它自己的值。我有代码可以做到这一点,但没有足够的技能将其转换为函数。以下是我已经测试过的代码,它本身运行良好: SELECT CASE WHEN 0 = CHARINDEX(' ',StageThree.REST_OF_NAME) THEN StageThree.REST_OF_NAME --No space? return the whole thing ELSE SUBSTRING(

我可以使用一些帮助来创建一个函数,该函数将解析或拆分字符串的名字作为它自己的值。我有代码可以做到这一点,但没有足够的技能将其转换为函数。以下是我已经测试过的代码,它本身运行良好:

SELECT
   CASE WHEN 0 = CHARINDEX(' ',StageThree.REST_OF_NAME)
         THEN StageThree.REST_OF_NAME --No space? return the whole thing
         ELSE SUBSTRING(StageThree.REST_OF_NAME, 1, CHARINDEX(' ',StageThree.REST_OF_NAME)-1)
         END AS FirstName

             FROM
            (SELECT
              --if the first three characters are in this list,
              --then pull it as a "StageThree".  otherwise return NULL for StageThree.
              CASE WHEN SUBSTRING(StageTwo.FullName,1,3) IN ('MR ','MS ','DR ','MRS')
                   THEN LTRIM(RTRIM(SUBSTRING(StageTwo.FullName,1,3)))
                   ELSE NULL
    END AS Title

  --if you change the list, don't forget to change it here, too.
  --so much for the DRY prinicple...
 ,CASE WHEN SUBSTRING(StageTwo.FullName,1,3) IN ('MR ','MS ','DR ','MRS')
       THEN LTRIM(RTRIM(SUBSTRING(StageTwo.FullName,4,LEN(StageTwo.FullName))))
       ELSE LTRIM(RTRIM(StageTwo.FullName))
       END AS REST_OF_NAME
 ,StageTwo.OriginalName

FROM
  (SELECT
    --trim leading & trailing spaces before trying to process
    --disallow extra spaces *within* the name
    REPLACE(REPLACE(REPLACE(REPLACE(LTRIM(RTRIM(Contact)),'  ',' '),'  ',' '),',',' '),'.',' ') AS FullName
    ,Contact AS OriginalName
  FROM
    My.dbo.database
  ) StageTwo
) StageThree
如果有人能帮我把它转换成函数,我将不胜感激。如果我能得到帮助,我也会发布代码来获取姓氏(作为一个函数)

这段代码似乎是正确的,但我无法在查询中启动它

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER FUNCTION [dbo].[FirstName](@input VARCHAR(100)) --how ever many you     need
RETURNS @returnTable table(FirstName varchar(100))

AS

BEGIN
INSERT INTO @returnTable (FirstName)
SELECT

   CASE WHEN 0 = CHARINDEX(' ',StageThree.REST_OF_NAME)
         THEN StageThree.REST_OF_NAME --No space? return the whole thing
         ELSE SUBSTRING(StageThree.REST_OF_NAME, 1, CHARINDEX(' ',StageThree.REST_OF_NAME)-1)
         END AS FirstName

             FROM
            (SELECT
              --if the first three characters are in this list,
              --then pull it as a "StageThree".  otherwise return NULL for StageThree.
              CASE WHEN SUBSTRING(StageTwo.FullName,1,3) IN ('MR ','MS ','DR ','MRS')
                   THEN LTRIM(RTRIM(SUBSTRING(StageTwo.FullName,1,3)))
                   ELSE NULL
                    END AS Title

  --if you change the list, don't forget to change it here, too.
  --so much for the DRY prinicple...
 ,CASE WHEN SUBSTRING(StageTwo.FullName,1,3) IN ('MR ','MS ','DR ','MRS')
       THEN LTRIM(RTRIM(SUBSTRING(StageTwo.FullName,4,LEN(StageTwo.FullName))))
       ELSE LTRIM(RTRIM(StageTwo.FullName))
       END AS REST_OF_NAME

FROM
  (SELECT
    --trim leading & trailing spaces before trying to process
    --disallow extra spaces *within* the name
    REPLACE(REPLACE(REPLACE(REPLACE(LTRIM(RTRIM(@Input)),'  ',' '),'  ',' '),',',' '),'.',' ') AS FullName

  ) StageTwo
) StageThree
RETURN

您需要一个表值函数

CREATE FUNCTION schema.your_function_name(@parameter datatype()) --how ever many you need
RETURNS @returnTable table(
TheTitle varchar(256),
REST_OF_NAME varchar(256),
OriginalName varchar(256)
)

AS

BEGIN
INSERT INTO @returnTable (TheTitle, REST_OF_NAME, OriginalName)
SELECT
...your code...
RETURN
END

经过更多的试验,我能够将其重写为标量函数,如下所示。正如所承诺的,我也张贴了姓氏的代码。这两者都不是万无一失的,而且这是为特定应用程序定制的(因此术语“blank”而不是NULL值)。但是,如果您需要将收集的数据作为单个字符串移动到需要名字和姓氏的系统中,这将非常有效。如果你提出了一些想法来克服一些其他的警告,请分享你的名字

谢谢西蒙的帮助

 CREATE FUNCTION [dbo].[getFirstName](@input VARCHAR(100)) 
 RETURNS   VARCHAR(100)

 AS

 BEGIN
 DECLARE @Result as Varchar(100)
 SET @Result = (
 SELECT

   CASE WHEN 0 = CHARINDEX(' ',StageThree.REST_OF_NAME)
         THEN StageThree.REST_OF_NAME --No space? return the whole thing
         ELSE SUBSTRING(StageThree.REST_OF_NAME, 1, CHARINDEX(' ',StageThree.REST_OF_NAME)-1)
         END AS FirstName

             FROM
            (SELECT
              --if the first three characters are in this list,
              --then pull it as a "StageThree".  otherwise return NULL for StageThree.
              CASE WHEN SUBSTRING(StageTwo.FullName,1,3) IN ('MR ','MS ','DR ','MRS')
                   THEN LTRIM(RTRIM(SUBSTRING(StageTwo.FullName,1,3)))
                   ELSE NULL
                    END AS Title

  --if you change the list, don't forget to change it here, too.
  --so much for the DRY prinicple...
 ,CASE WHEN SUBSTRING(StageTwo.FullName,1,3) IN ('MR ','MS ','DR ','MRS')
       THEN LTRIM(RTRIM(SUBSTRING(StageTwo.FullName,4,LEN(StageTwo.FullName))))
       ELSE LTRIM(RTRIM(StageTwo.FullName))
       END AS REST_OF_NAME

FROM
  (SELECT
    --trim leading & trailing spaces before trying to process
    --disallow extra spaces *within* the name
    REPLACE(REPLACE(REPLACE(REPLACE(LTRIM(RTRIM(@Input)),'  ',' '),'  ',' '),',',' '),'.',' ') AS FullName

  ) StageTwo
) StageThree )
 RETURN @Result
 END
和LastName函数:

CREATE FUNCTION [dbo].[getLastName](@input VARCHAR(100)) 
RETURNS VARCHAR(100)
AS

BEGIN
 DECLARE @Result as Varchar(100)
 SET @Result = (


SELECT 

   CASE WHEN SUBSTRING(StageFour.REST_OF_NAME, 1 + CHARINDEX(' ',StageFour.REST_OF_NAME),LEN(StageFour.REST_OF_NAME)) IN ('jr','sr','II','III','IV')
        THEN SUBSTRING(StageFour.REST_OF_NAME, 1 , CHARINDEX(' ',StageFour.REST_OF_NAME)-1) + ' ' +
             SUBSTRING(StageFour.REST_OF_NAME, 1 + CHARINDEX(' ',StageFour.REST_OF_NAME),LEN(StageFour.REST_OF_NAME))
        WHEN SUBSTRING(StageFour.REST_OF_NAME, 1 + CHARINDEX(' ',StageFour.REST_OF_NAME),LEN(StageFour.REST_OF_NAME)) IS NULL THEN '(BLANK)'
        ELSE SUBSTRING(StageFour.REST_OF_NAME, 1 + CHARINDEX(' ',StageFour.REST_OF_NAME),LEN(StageFour.REST_OF_NAME))
        END AS LastName

 FROM
      (SELECT
        StageThree.Title
       ,CASE WHEN 0 = CHARINDEX(' ',StageThree.REST_OF_NAME)
             THEN StageThree.REST_OF_NAME --No space? return the whole thing
             WHEN StageThree.REST_OF_NAME IS NULL THEN '(Blank)'
             ELSE SUBSTRING(StageThree.REST_OF_NAME, 1, CHARINDEX(' ',StageThree.REST_OF_NAME)-1)
             END AS FirstName
       ,CASE WHEN 0 = CHARINDEX(' ',StageThree.REST_OF_NAME)  
             THEN NULL  --no spaces @ all?  then 1st name is all we have
             ELSE SUBSTRING(StageThree.REST_OF_NAME, CHARINDEX(' ',StageThree.REST_OF_NAME)+1, LEN(StageThree.REST_OF_NAME))
             END AS REST_OF_NAME
       ,StageThree.FullName
                 FROM
                (SELECT
                  --if the first three characters are in this list,
                  --then pull it as a "StageThree".  otherwise return NULL for StageThree.
                  CASE WHEN SUBSTRING(StageTwo.FullName,1,3) IN ('MR ','MS ','DR ','MRS')
                       THEN LTRIM(RTRIM(SUBSTRING(StageTwo.FullName,1,3)))
                       ELSE NULL
        END AS Title

      --if you change the list, don't forget to change it here, too.
      --so much for the DRY prinicple...
     ,CASE WHEN SUBSTRING(StageTwo.FullName,1,3) IN ('MR ','MS ','DR ','MRS')
           THEN LTRIM(RTRIM(SUBSTRING(StageTwo.FullName,4,LEN(StageTwo.FullName))))
           ELSE LTRIM(RTRIM(StageTwo.FullName))
           END AS REST_OF_NAME
     ,StageTwo.FullName
    FROM
      (SELECT
        --trim leading & trailing spaces before trying to process
        --disallow extra spaces *within* the name
        REPLACE(REPLACE(REPLACE(REPLACE(LTRIM(RTRIM(@Input)),'  ',' '),'  ',' '),',',' '),'.',' ') AS FullName

      ) StageTwo
    ) StageThree
  ) StageFour
)

RETURN @Result
END

为什么这很难转换成函数。你读过关于函数的文档了吗?如果你使用Google,网站上有很多关于这个的资源。请告诉我们你试图把它变成一个隐蔽函数,并指出你有什么问题。谢谢,这在创建函数时起到了作用。我只需要让它在我的查询中运行。我欣赏这个有用的答案。
SELECT*FROM dbo.your_函数(varible1,varible2)
myah-Ha时刻。这与标量函数的工作原理非常不同。这就是为什么我想不出来。谢谢你的帮助!