如何在 SQL Server 2005 中对记录进行分块?

发布于 2024-10-21 02:09:41 字数 300 浏览 5 评论 0原文

假设我在 tableA 中有一个列 Name

Name
jennifer Hughs
Mike nadrotosky
Arnold Woods
Raj Jai Soni

,那么我如何将这些结果放入单独的 3 列中,就像

FirstName MiddleName LastName
Jennifer              Hughs
.
.
.
Raj        Jai       Soni

我尝试使用 Substring 但不起作用一样。

lets say i have a column Name in tableA

Name
jennifer Hughs
Mike nadrotosky
Arnold Woods
Raj Jai Soni

so how do i put these results in seperate 3 columns like

FirstName MiddleName LastName
Jennifer              Hughs
.
.
.
Raj        Jai       Soni

I tried with Substring but not working.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

为你鎻心 2024-10-28 02:09:42

嗯,它看起来比实际上容易得多 - 特别是如果你开始拥有多个名字和/或中间名的人。

我的方法是创建一个封装该逻辑的存储函数 - 仅在内联 T-SQL 语句中将其拼写出来实际上并不那么简单。

这是第一次尝试 - 只要您没有多个中间名,就可以工作:

CREATE FUNCTION dbo.SplitName(@InputName VARCHAR(200))
RETURNS @nameParts TABLE 
(
    FirstName VARCHAR(100),
    MiddleName VARCHAR(100),
    LastName VARCHAR(100)
)
AS BEGIN
    DECLARE @FirstSpace INT, @LastSpace INT

    SET @FirstSpace = CHARINDEX(' ', @InputName)
    SET @LastSpace = CHARINDEX(' ', @InputName, @FirstSpace+1)

    INSERT INTO @nameParts(FirstName, MiddleName, LastName)
        SELECT
            SUBSTRING(@InputName, 1, @FirstSpace),
            CASE @LastSpace 
                WHEN 0 THEN CAST(NULL AS VARCHAR(100))
                ELSE SUBSTRING(@InputName, @FirstSpace+1, @LastSpace - @FirstSpace)
            END,
            CASE @LastSpace 
                WHEN 0 THEN SUBSTRING(@InputName, @FirstSpace+1, 999)
                ELSE SUBSTRING(@InputName, @LastSpace, 999)
            END

    RETURN
END

如果您使用一些示例数据对此进行测试,您将得到以下结果:

DECLARE @nameTable TABLE (NameValue VARCHAR(100))

INSERT INTO @nametable VALUES('Jennifer Hughes')
INSERT INTO @nametable VALUES('Mike Nadrotosky')
INSERT INTO @nametable VALUES('Arnold Woods')
INSERT INTO @nametable VALUES('Raj Jai Soni')

SELECT *
FROM @nameTable
CROSS APPLY dbo.SplitName(nameValue)

并且该 SELECT 的输出是:

NameValue          FirstName   MiddleName    LastName
Jennifer Hughes    Jennifer       NULL       Hughes
Mike Nadrotosky    Mike           NULL       Nadrotosky
Arnold Woods       Arnold         NULL       Woods
Raj Jai Soni       Raj            Jai        Soni

Well, it looks at lot easier than it really is - especially if you start having people with multiple first- and/or middle names.

My approach would be to create a stored function that encapsulates that logic - it's really not that simple to just spell it out in an inline T-SQL statement.

Here's a first attempt - works as long as you don't have more than a single middle name:

CREATE FUNCTION dbo.SplitName(@InputName VARCHAR(200))
RETURNS @nameParts TABLE 
(
    FirstName VARCHAR(100),
    MiddleName VARCHAR(100),
    LastName VARCHAR(100)
)
AS BEGIN
    DECLARE @FirstSpace INT, @LastSpace INT

    SET @FirstSpace = CHARINDEX(' ', @InputName)
    SET @LastSpace = CHARINDEX(' ', @InputName, @FirstSpace+1)

    INSERT INTO @nameParts(FirstName, MiddleName, LastName)
        SELECT
            SUBSTRING(@InputName, 1, @FirstSpace),
            CASE @LastSpace 
                WHEN 0 THEN CAST(NULL AS VARCHAR(100))
                ELSE SUBSTRING(@InputName, @FirstSpace+1, @LastSpace - @FirstSpace)
            END,
            CASE @LastSpace 
                WHEN 0 THEN SUBSTRING(@InputName, @FirstSpace+1, 999)
                ELSE SUBSTRING(@InputName, @LastSpace, 999)
            END

    RETURN
END

If you test this with some sample data, you'll get the following results:

DECLARE @nameTable TABLE (NameValue VARCHAR(100))

INSERT INTO @nametable VALUES('Jennifer Hughes')
INSERT INTO @nametable VALUES('Mike Nadrotosky')
INSERT INTO @nametable VALUES('Arnold Woods')
INSERT INTO @nametable VALUES('Raj Jai Soni')

SELECT *
FROM @nameTable
CROSS APPLY dbo.SplitName(nameValue)

and the output for that SELECT is:

NameValue          FirstName   MiddleName    LastName
Jennifer Hughes    Jennifer       NULL       Hughes
Mike Nadrotosky    Mike           NULL       Nadrotosky
Arnold Woods       Arnold         NULL       Woods
Raj Jai Soni       Raj            Jai        Soni
私野 2024-10-28 02:09:42

@marc_s 的答案是最完整的。有几次我不得不这样做,它们都是一次性数据加载类型的作业。如果您的情况相同,我强烈建议您在 Excel 中进行操作!

  1. 将表从 Management studio 复制并粘贴到 Excel
  2. 使用文本到列
  3. 对第 3 列上的整行重新排序,以获取顶部没有中间名的所有内容
  4. 选择第 3 列为空的所有内容并移动值从第 2 列到第 3 列
  5. 将数据导入到目标 SQL 表

为了在 T-SQL 中执行此操作,我已使用 SQL Sharp。然而,这最终会产生与@marc_s类似的结果,所以我不会在这里讨论它。

@marc_s answer is the most complete. The few times I've had to do this they've been as one-off data load type jobs. If yours is the same I would strongly suggest just doing it in Excel!

  1. Copy and paste the table from Management studio in to Excel
  2. Use text to columns
  3. Re-sort the whole row on column 3 to get all of those with no middle name at the top
  4. Select all of the ones with an empty column 3 and move the values from Column 2 in to column 3
  5. Import the data in to the destination SQL Table

For doing this in T-SQL I have done it using the string split function in SQL Sharp. This will however ultimately yield similar results to @marc_s so I won't go in to it here.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文