拆分全名并将其写入SQL Server 2008中的另一个表 [英] Splitting the full name and writing it to another table in SQL Server 2008

查看:148
本文介绍了拆分全名并将其写入SQL Server 2008中的另一个表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个表格,说 A ,其中有一列「FULLNAME」。 此列下存储的值的格式为surname name middle_name(每个值之间有一个空格)。我有另一个表 B ,其中有列 SURNAME NAME MIDDLENAME 。从表格 A 中取出所有 FULLNAME 单元格的最佳方法是什么,将它们相应拆分并将它们插入表格 B 中?



感谢

解决方案

一个字符串(通常返回它的索引)与Substring函数,除了你将需要Left和Right函数



例如在SQL Server中你会发现函数: / p>

CHARINDEX(expressionToFind,expressionToSearch [,start_location])



SUBSTRING(表达式,开始,长度)



LEFT(character_expression,integer_expression)



RIGHT(character_expression,integer_expression)



步骤:


  1. 使用LEFT获得第一个字(integer_expression =第一个
    的空间的索引)

  2. 使用Substring获取中间单词(开始是第一个索引
    Emtpy空间+ 1,长度是整个长度 -
    的第二个索引的emtpy空间,使用startlocation搜索第二次出现应该是第一次出现+1)

  3. 使用正确的函数获取与步骤1类似的最后一个字词

请注意,如果你有任何名称,包括在中间的空格(例如一个名字像安娜玛丽亚),这将无法正常工作。


I have a table, say A, in which there is a column FULLNAME. Values stored under this column are in the format of "surname name middle_name" (with one space between each). And I have another table B, in which I have columns SURNAME, NAME and MIDDLENAME. What would be the best way to take all of the FULLNAME cells from the table A, split them accordingly and insert them into the table B?

Thanks

解决方案

You can combine functions for searching an occurence in a string (which return normally its index) with the Substring function, besides you will need the Left and Right functions

For example in SQL Server you will find the functions:

CHARINDEX ( expressionToFind ,expressionToSearch [ , start_location ] )

SUBSTRING ( expression ,start , length )

LEFT ( character_expression , integer_expression )

RIGHT ( character_expression , integer_expression )

STEPS:

  1. Use the LEFT to get the 1st word (integer_expression = index of 1st Emtpy space)
  2. Use Substring to get the middle word (start is the index of 1st Emtpy space + 1 , length is the entire length - the second index of the emtpy space, use the startlocation to search the second occurence which should be the first occurence +1)
  3. Use the right function to get the last word similar to step 1

Notice that if you have any names including empty spaces in the middle (example a first name like anna maria) this wouldnt work as expected.

这篇关于拆分全名并将其写入SQL Server 2008中的另一个表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆