pbootcms网站模板|日韩1区2区|织梦模板||网站源码|日韩1区2区|jquery建站特效-html5模板网

SQL:交叉應(yīng)用將名稱拆分為名字、姓氏和 MI

SQL: cross apply to split names into first, last, and MI(SQL:交叉應(yīng)用將名稱拆分為名字、姓氏和 MI)
本文介紹了SQL:交叉應(yīng)用將名稱拆分為名字、姓氏和 MI的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我有一個包含這樣的用戶名的表.

I have a table that has user names like this.

Name
-----
Smith-Bay, Michael R.
Abbott, David Jr.
Actor, Cody
Agular, Stephen V.

我需要名字看起來像:

Last         First    MI
-------------------------
Smith-Bay    Michael  R
Abbott       David    Jr
Actor        Cody
Agular       Stephen  V 

我有以下 SQL 將名稱拆分為第一個和最后一個:

I have the following SQL that splits the name into first and last:

select vl.lastname, vf.firstname
from users as t cross apply
(values (left(t.name, charindex(', ', t.name)), stuff(t.name, 1, 
charindex(', ', t.name) + 1, ''))) vl(lastname, rest) 
cross apply 
(values (left(vl.rest, charindex(' ', vl.rest + ' ')))) vf(firstname)
order by  vl.lastname

如何應(yīng)用另一個交叉應(yīng)用來提取名字減去末尾句點之后的所有內(nèi)容?

How can I apply another cross apply to extract basically everything after the first name minus the period at the end?

推薦答案

我不得不多次這樣做,因為我經(jīng)常使用 ETL 并且由于數(shù)據(jù)存儲不佳而需要從字符串中提取項目或者只是簡單地從報告中提取數(shù)據(jù).數(shù)據(jù)并不總是很好地打包在單獨的列中,我發(fā)現(xiàn)自己出于各種原因解析數(shù)據(jù).希望您解析的數(shù)據(jù)是一致的.不一致的數(shù)據(jù)要么使這變得更加困難,要么不可能.如果您的名字完全符合您建議的格式,那么我下面的方法將非常有效.我在很多場合都用過它.

I've had to do this on many occasions as I work ETL on a regular basis and either need to extract items from within strings due to either bad data storage or just simply having to pull the data from reports. The data isn't always nicely packaged in separate columns and I find myself parsing data for all sorts of reasons. Hopefully the data you are parsing is consistent. Inconsistent data either makes this much more difficult or impossible. If you can rely on your names being exactly in the format you suggested my method below will work perfectly. I've used it on many occasions.

下面的方法我在許多不同的語言中都使用過.我已經(jīng)在 MS ACCESS、Microsoft SSMS 和 C# 中完成了這項工作.我的例子來自 Oracle.

The method below I've used in many different languages. I've done this in MS ACCESS, Microsoft SSMS and C#. My example is out of Oracle.

基本思想是:

找到分隔你的 First_Name、Last_Name 和 Middle_Initial 字符串的字符位置.

使用獲得的字符位置將字符串提取到新列中.

代碼如下:

WITH character_pos AS
(
/* First we need the character positions for spaces, commas and the period for the middle initial */
SELECT name
  /* Find 1st Space in the name so we can extract the first name from the string */
  , instr(name, ', ') AS comma_1st_space_pos
  /* Find 2nd Space in the name so we can extract the last name from the string */
  , instr(name, ' ', 1, 2) AS comma_2nd_space_pos
  /* Get the Length of the last name so we know how many characters the substr function should extract */
  , instr(name, ' ', 1, 2) - (instr(name, ', ') + 2) AS last_name_length
  /* Find period in the name so we can extract the Middle Initial should it exist */
  , instr(name, '.')  AS period_pos
  , (instr(name, '.') - 1) - instr(name, ' ', 1, 2) AS middle_initial_length
  
FROM parse_name
) /* END character_pos CTE */

SELECT name  
  , substr(name, 0, comma_1st_space_pos -1) AS last_name
   
  , CASE WHEN  period_pos = 0 THEN substr(name, comma_1st_space_pos + 2)
    ELSE substr(name, comma_1st_space_pos + 2, last_name_length) 
    END AS first_name
   
  , substr(name, comma_2nd_space_pos + 1, middle_initial_length) AS middle_initial
  
  , comma_1st_space_pos, comma_2nd_space_pos, last_name_length
  , period_pos, middle_initial_length
FROM character_pos
;

我使用 CTE 只是為了在實際提取之外組織字符位置,但這一切都可以在一個 SQL 語句中完成.

I used a CTE just to organize the character positions outside of the actual extraction however this all could be done in one single SQL Statement.

基本上,這證明除了一些簡單的字符串解析函數(shù)之外,您不需要任何額外的東西.您只需要 Instring 和 Substring,它們通常以任何語言提供.沒有存儲過程,沒有臨時表,也不需要額外的外部代碼.除非有超出原始問題范圍的其他因素導致必須使用 SQL 以外的任何其他內(nèi)容.

Basically this proves you don't need anything extra outside of just some simple string parsing functions. All you need is Instring and Substring which are usually available in any language. No Stored procedures, no temp table and no extra outside code needed. Unless there are other factors outside the scope of the original question that makes it necessary to use anything other than just SQL.

這篇關(guān)于SQL:交叉應(yīng)用將名稱拆分為名字、姓氏和 MI的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

Modify Existing decimal places info(修改現(xiàn)有小數(shù)位信息)
The correlation name #39;CONVERT#39; is specified multiple times(多次指定相關(guān)名稱“CONVERT)
T-SQL left join not returning null columns(T-SQL 左連接不返回空列)
remove duplicates from comma or pipeline operator string(從逗號或管道運算符字符串中刪除重復項)
Change an iterative query to a relational set-based query(將迭代查詢更改為基于關(guān)系集的查詢)
concatenate a zero onto sql server select value shows 4 digits still and not 5(將零連接到 sql server 選擇值仍然顯示 4 位而不是 5)
主站蜘蛛池模板: 校园文化空间设计-数字化|中医文化空间设计-党建|法治廉政主题文化空间施工-山东锐尚文化传播公司 | 深圳市宏康仪器科技有限公司-模拟高空低压试验箱-高温防爆试验箱-温控短路试验箱【官网】 | 脱硫搅拌器厂家-淄博友胜不锈钢搅拌器厂家 | 沈阳楼承板_彩钢板_压型钢板厂家-辽宁中盛绿建钢品股份有限公司 轴承振动测量仪电箱-轴承测振动仪器-测试仪厂家-杭州居易电气 | 工业铝型材生产厂家_铝合金型材配件批发精加工定制厂商 - 上海岐易铝业 | 广东风淋室_广东风淋室厂家_广东风淋室价格_广州开源_传递窗_FFU-广州开源净化科技有限公司 | 耐酸泵,耐腐蚀真空泵,耐酸真空泵-淄博华舜耐腐蚀真空泵有限公司 精密模具-双色注塑模具加工-深圳铭洋宇通 | 恒压供水控制柜|无负压|一体化泵站控制柜|PLC远程调试|MCGS触摸屏|自动控制方案-联致自控设备 | 电加热导热油炉-空气加热器-导热油加热器-翅片电加热管-科安达机械 | 兰州UPS电源,兰州山特UPS-兰州万胜商贸| 滚塑PE壳体-PE塑料浮球-警示PE浮筒-宁波君益塑业有限公司 | 艺术漆十大品牌_艺术涂料加盟代理_蒙太奇艺术涂料厂家品牌|艺术漆|微水泥|硅藻泥|乳胶漆 | 破碎机_上海破碎机_破碎机设备_破碎机厂家-上海山卓重工机械有限公司 | 飞歌臭氧发生器厂家_水处理臭氧发生器_十大臭氧消毒机品牌 | 电机保护器-电动机综合保护器-上海硕吉电器有限公司 | 辊道窑炉,辊道窑炉厂家-山东艾希尔 | 密封圈_泛塞封_格莱圈-[东莞市国昊密封圈科技有限公司]专注密封圈定制生产厂家 | 上海三信|ph计|酸度计|电导率仪-艾科仪器| 超声波成孔成槽质量检测仪-压浆机-桥梁预应力智能张拉设备-上海硕冠检测设备有限公司 | 最新范文网_实用的精品范文美文网| 德国EA可编程直流电源_电子负载,中国台湾固纬直流电源_交流电源-苏州展文电子科技有限公司 | DNA亲子鉴定_DNA基因检测中心官方预约平台-严选好基因网 | 伶俐嫂培训学校_月嫂培训班在哪里报名学费是多少_月嫂免费政府培训中心推荐 | 济南画室培训-美术高考培训-山东艺霖艺术培训画室 | 浙江富广阀门有限公司| 加盟店-品牌招商加盟-创业项目商机平台 | 保定市泰宏机械制造厂-河北铸件厂-铸造厂-铸件加工-河北大件加工 | 餐饮加盟网_特色餐饮加盟店_餐饮连锁店加盟 | 篷房[仓储-婚庆-展览-活动]生产厂家-江苏正德装配式帐篷有限公司 | 巨野月嫂-家政公司-巨野县红墙安康母婴护理中心 | 淄博不锈钢,淄博不锈钢管,淄博不锈钢板-山东振远合金科技有限公司 | 废水处理-废气处理-工业废水处理-工业废气处理工程-深圳丰绿环保废气处理公司 | 校园文化空间设计-数字化|中医文化空间设计-党建|法治廉政主题文化空间施工-山东锐尚文化传播公司 | 工业胀紧套_万向节联轴器_链条-规格齐全-型号选购-非标订做-厂家批发价格-上海乙谛精密机械有限公司 | 辐射仪|辐射检测仪|辐射巡测仪|个人剂量报警仪|表面污染检测仪|辐射报警仪|辐射防护网 | ICP备案查询_APP备案查询_小程序备案查询 - 备案巴巴 | 金环宇|金环宇电线|金环宇电缆|金环宇电线电缆|深圳市金环宇电线电缆有限公司|金环宇电缆集团 | 气动|电动调节阀|球阀|蝶阀-自力式调节阀-上海渠工阀门管道工程有限公司 | 在线PH计-氧化锆分析仪-在线浊度仪-在线溶氧仪- 无锡朝达 | 厦门网站建设_厦门网站设计_小程序开发_网站制作公司【麦格科技】 | RFID电子标签厂家-上海尼太普电子有限公司 |