如果我有一个包含完整名称的列的表,例如:
fullname
------------
Joe Bloggs
Peter Smith
Mary Jones and Liz Stone
如何使用SQL重试全名列中每个条目的名字和姓氏。我并不担心我的例子中的第3个条目中的第二个名字,即Liz Stone。
所以基本上要检索
Firstname
---------
Joe
Peter
Mary
Lastname
--------
Bloggs
Smith
Jones
答案 0 :(得分:10)
这是一个SQL Server 2016之前的方法,它使用基本的字符串函数来隔离名字和姓氏。
SELECT SUBSTRING(fullname, 1, CHARINDEX(' ', fullname) - 1) AS Firstname,
SUBSTRING(fullname,
CHARINDEX(' ', fullname) + 1,
LEN(fullname) - CHARINDEX(' ', fullname)) AS Lastname
FROM yourTable
请注意,此解决方案假定fullname
列仅包含单个名字和单个姓氏(即没有中间名,缩写等)。
答案 1 :(得分:4)
这是一个滑坡,没有简单的答案。也就是说,请考虑以下内容
private Uri uri;
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case FILE_SELECT_CODE:
if (resultCode == RESULT_OK) {
// Get the Uri of the selected file
uri = data.getData();
}
break;
}
super.onActivityResult(requestCode, resultCode, data);
}
public void sendAttachment() {
File filePhoto = new File(uri.getPath());
Boolean fileIsPublic = false;
QBContent.uploadFileTask(filePhoto, fileIsPublic, null, new QBProgressCallback() {
@Override
public void onProgressUpdate(int i) {
// i - progress in percentages
Log.e(TAG, "onProgressUpdate: " + i);
}
}).performAsync(new QBEntityCallback<QBFile>() {
@Override
public void onSuccess(QBFile qbFile, Bundle bundle) {
Integer id = qbFile.getId();
// create a message
QBChatMessage chatMessage = new QBChatMessage();
chatMessage.setProperty("save_to_history", "1"); // Save a message to history
// attach a photo
QBAttachment attachment = new QBAttachment("photo");
attachment.setId(qbFile.getId().toString());
chatMessage.setBody("");
chatMessage.addAttachment(attachment);
// send a message
try {
mQBChatDialog.sendMessage(chatMessage);
mtvEmpty.setVisibility(View.INVISIBLE);
} catch (SmackException.NotConnectedException e) {
e.printStackTrace();
}
}
@Override
public void onError(QBResponseException errors) {
Log.e("DATA", TextUtils.join(",", errors.getErrors()));
}
});
}
返回
Declare @YourTable table (FullName varchar(50))
Insert Into @YourTable values
('Joe Bloggs'),
('Peter Smith'),
('Betty Jane Martinez'),
('Mary Jones and Liz Stone')
Select A.*
,FirstName = Pos1+case when Pos3 is not null then ' '+Pos2 else '' end
,LastName = case when Pos3 is null then Pos2 else Pos3 end
From @YourTable A
Cross Apply (
Select Pos1 = xDim.value('/x[1]','varchar(max)')
,Pos2 = xDim.value('/x[2]','varchar(max)')
,Pos3 = xDim.value('/x[3]','varchar(max)')
,Pos4 = xDim.value('/x[4]','varchar(max)')
,Pos5 = xDim.value('/x[5]','varchar(max)')
,Pos6 = xDim.value('/x[6]','varchar(max)')
From (Select Cast('<x>' + replace((Select substring(FullName,1,charindex(' and ',FullName+' and ')-1) as [*] For XML Path('')),' ','</x><x>')+'</x>' as xml) as xDim) as A
) B
如果它对视觉有帮助,CROSS APPLY会生成
答案 2 :(得分:1)
SELECT CASE
WHEN CHARINDEX(' ', FullName) > 0
THEN SUBSTRING(FullName, 1, LEN(FullName) - CHARINDEX(' ', REVERSE(FullName)))
ELSE ''
END AS FirstName,
CASE
WHEN CHARINDEX(' ', FullName) > 0
THEN REVERSE(SUBSTRING(REVERSE(FullName),
1,
CHARINDEX(' ', REVERSE(FullName)) - 1))
ELSE FullName
END AS LastName
FROM(VALUES('Mary Anne Bloggs'), ('Joe Bloggs'), ('Bloggs')) AS T(FullName);
此版本检查全名中是否有空格可分割。如果没有,则将名字设置为空字符串,并将全名放入姓氏中。另外,当有多个空格时,使用反向可在最后一个空格上分割
答案 3 :(得分:1)
BigQuery:标准Sql
substr(name,1,STRPOS(name,'')-1)作为名字,
substr(name,STRPOS(name,'')+ 1,length(name))作为姓氏
答案 4 :(得分:0)
这是这个问题中最容易和最短的,没有任何假设。您还可以使用rtrim(ltrim('firstname lastname'))
进一步增强此功能。
如果字符串前面有空格,
Select
substring('Firstname Lastname',1,CHARINDEX(' ', 'Firstname Lastname')) as firstname,
substring('Firstname Lastname',CHARINDEX(' ', 'Firstname Lastname'),LEN('Firstname Lastname')) as Lastname
答案 5 :(得分:0)
我使用此查询来检索first和lastname
SELECT SUBSTRING(FULLNAME, 1, CASE WHEN CHARINDEX(' ', FULLNAME)>0 THEN CHARINDEX(' ', FULLNAME) - 1 ELSE LEN(FULLNAME) END ) AS Firstname,
REVERSE(SUBSTRING(REVERSE(FULLNAME), 1, CASE WHEN CHARINDEX(' ', REVERSE(FULLNAME))>0 THEN CHARINDEX(' ', REVERSE(FULLNAME)) - 1 ELSE LEN(REVERSE(FULLNAME)) END ) )AS Firstname FROM HRMDESFO.EMPLOID
答案 6 :(得分:0)
select passemail,substring(passemail,1,instr(passemail,'@') - 1) as name ,
substring(passemail,instr(passemail,'@') + 1,length(passemail)) from passenger
答案 7 :(得分:0)
用于获取名字
SELECT SUBSTR(FULLNAME,1,(LOCATE(' ',FULLNAME))) AS FIRSTTNAME from EmployeeDetails;
姓氏
SELECT SUBSTR(FULLNAME,(LOCATE(' ',FULLNAME))) AS LASTNAME from EmployeeDetails;
SO
SELECT SUBSTR(FULLNAME,1,(LOCATE(' ',FULLNAME))) AS FIRSTTNAME, SUBSTR(FULLNAME,(LOCATE(' ',FULLNAME))) AS LASTNAME from EmployeeDetails;
答案 8 :(得分:0)
SELECT
LEFT(column_name, POSITION(' ' IN column_name)-1) AS first_name,
RIGHT(column_name, LENGTH(column_name) - POSITION(' ' IN column_name)) AS last_name
FROM table_name
答案 9 :(得分:0)
SELECT SUBSTR(NAME,1,(LOCATE(NAME, ' '))) AS FIRSTTNAME, SUBSTR(NAME,(LOCATE(NAME, ' ')+1)) AS LASTNAME 从 yourTABLE;
答案 10 :(得分:-2)
你可以使用,
STRING_SPLIT (string , separator)