简体   繁体   English

SQL 新手:仅从全名字段导入名字到报表生成器(数据源查询)

[英]SQL Newbie: Import First Name Only from Full Name Field to Report Builder (Data Source Query)

I'm sure this is probably a very simple question but I'm new to SQL and I can't find anything on the forum that addresses exactly what I'm trying to accomplish, so bear with me.我确信这可能是一个非常简单的问题,但我是 SQL 的新手,我在论坛上找不到任何可以准确解决我想要完成的内容的内容,所以请耐心等待。

I'm working in SQL Server Report Builder.我在 SQL 服务器报表生成器中工作。 I'm setting up my Data Source and Query.我正在设置我的数据源和查询。 The table I'm querying only has a "Full Name" field, but, on this report, I need to split it up into "First Name" and "Last Name".我正在查询的表只有一个“全名”字段,但是,在此报告中,我需要将其拆分为“名字”和“姓氏”。 I'd like to do all of this directly from the Dataset->Query section, if possible.如果可能的话,我想直接从 Dataset->Query 部分完成所有这些操作。 The "Full Name" field is in this format: SMITH, MARK ALEXANDER “全名”字段采用以下格式: SMITH, MARK ALEXANDER

Some people have more than one first name, so, in the example above "Mark Alexander" can become "First Name".有些人有多个名字,所以,在上面的例子中,“Mark Alexander”可以变成“First Name”。

I've been able to get this working for the Last Name using PARSENAME as so:我已经能够使用 PARSENAME 为姓氏工作,如下所示:

SELECT DISTINCT [Zip Code]
,PARSENAME(REPLACE([Full Name], ',', '.'), 2) AS [LastName]
FROM [SomeDB].[dbo].[T_MAIN_LIVE]

...but I have not been able to get it working correctly for the First Name. ...但我无法让它为名字正常工作。 I've also tried using the RIGHT command but had similar issues.我也尝试过使用 RIGHT 命令,但遇到了类似的问题。 It either cut off part of the first name, ignored "secondary" first names, or printed the first name with a preceding space that I can't seem to get rid of.它要么切断了名字的一部分,忽略了“次要”名字,要么打印了名字,前面有一个我似乎无法摆脱的空格。 For example, the closest I can get is with this:例如,我能得到的最接近的是:

SELECT DISTINCT [Zip Code]
,PARSENAME(REPLACE([Full Name], ',', '.'), 1) AS [FirstName]
FROM [SomeDB].[dbo].[T_MAIN_LIVE]

...but I'm left with a preceding space. ...但我留下了一个前面的空间。 Maybe I just need to eliminate the preceding space after the fact?也许我只需要事后消除前面的空格? If so, how should I go about that?如果是这样,我应该如何 go 关于那个?

Again, I'm sure this is a simple fix for you SQL experts so I apologize for my ignorance.同样,我确信这对您来说是一个简单的修复 SQL 专家所以我为我的无知道歉。

Thanks in advance, Cameron在此先感谢,卡梅伦

You can try this.你可以试试这个。 It worked for me.它对我有用。

SELECT name = 'smith, mark alexander'
INTO #name

SELECT concat(SUBSTRING(name, CHARINDEX(',', name, 1) + 1, 20), ' ', SUBSTRING(name, 1, CHARINDEX(',', name, 1) - 1))
FROM #name

EDIT: This assumes there is a comma separating the last from the first name in all instances.编辑:这假设在所有情况下都有一个逗号分隔姓氏和名字。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM