我正在处理一个查询,该查询从一列返回前两个名称。我通过拾取第一次出现的,
周围的所有内容来实现此目的,这是名称的分隔符。
这是Functioning version @ SQL Fiddle。
该表看起来与此类似:
| Director | CriteriaData |
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Director1|NameA Surname A, NameB SurnameB, NameC SurnameC |
| Director2|NameA Surname A, NameB SurnameB, NameC SurnameC |
| Director3|NameA Surname A, NameB SurnameB, NameC SurnameC |
正常运行的查询是:
SELECT LEFT(CriteriaData, LEN(CriteriaData) - CHARINDEX(', ', CriteriaData)) AS "2 names"
FROM Table1
WHERE CriteriaData LIKE '%, %'
我想要实现的是有两个结果列。一个叫做
Director
,另一个叫上面的查询结果,叫做2 names
这是我失败的尝试:Test version,在此尝试将原始查询嵌套在新查询中。看起来像这样:
SELECT
Director,
(LEFT(CriteriaData, LEN(CriteriaData) - CHARINDEX(', ', CriteriaData))
FROM Table1
WHERE CriteriaData LIKE '%, %')AS "2 names"
FROM Table1
WHERE Director = 'Director3'
我想在结果表中看到
|Director | 2 Names |
||||||||||||||||||||||||||||||||||||||||||||
|Director3| NameA Surname A, NameB SurnameB|
我希望这有任何意义,并且我想要实现的目标是可行的。非常感谢您的阅读。我期待着您的建议。
最佳答案
也许我把问题弄错了,但这是您要找的东西吗?
SELECT
CriteriaData,
LEFT(CriteriaData, LEN(CriteriaData) - CHARINDEX(', ', CriteriaData)) AS "2 names"
FROM Table1
WHERE CriteriaData LIKE '%, %'
AND Director = 'Director3'