如何在 SQL Server 2005 中对记录进行分块?
假设我在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
嗯,它看起来比实际上容易得多 - 特别是如果你开始拥有多个名字和/或中间名的人。
我的方法是创建一个封装该逻辑的存储函数 - 仅在内联 T-SQL 语句中将其拼写出来实际上并不那么简单。
这是第一次尝试 - 只要您没有多个中间名,就可以工作:
如果您使用一些示例数据对此进行测试,您将得到以下结果:
并且该 SELECT 的输出是:
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:
If you test this with some sample data, you'll get the following results:
and the output for that SELECT is:
@marc_s 的答案是最完整的。有几次我不得不这样做,它们都是一次性数据加载类型的作业。如果您的情况相同,我强烈建议您在 Excel 中进行操作!
为了在 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!
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.