简体   繁体   English

连接 Oracle SQL 中名字和姓氏的第一个字符

[英]Concatenating the first character of first name and the last name in Oracle SQL

Question: New to Oracle SQL and cant figure this out.问题: Oracle SQL 的新手,无法弄清楚这一点。 Any Help is appreciated.任何帮助表示赞赏。 :) :)

In table employee2, generate the email address for column username for each student by concatenating the first character of employee's first name and the employee's last name.在表employee2 中,通过连接员工名字的第一个字符和员工的姓氏,为每个学生的username列生成email 地址。 For instance, the username of employee Peter Stone will be pstone.例如,员工 Peter Stone 的用户名将是 pstone。 NOTE: the username is in all lower case letters.注意:用户名全部为小写字母。

What I have Tried:我试过的:

select 
  concat(left(firstname, 1) + ' ',left(lastname)) UserName
from employee2;

select concat(substring(firstname, 1),substring(lastname)) UserName from employee2; select concat(substring(firstname, 1),substring(lastname)) 来自employee2的用户名;

You should use substr and concatanation operator ||您应该使用substr和串联运算符|| as follows:如下:

Update employee2
   Set username = lower(substr(firstname,1,1) || lastname)

Benifit of using ||使用的好处|| over concat function is that concat need to be called multiple times if there are more than two strings need to be combined.concat function 就是如果需要合并两个以上的字符串,需要多次调用concat || is string operator and easy to use and read.是字符串运算符,易于使用和阅读。

There are a few issues here:这里有几个问题:

  1. Oracle doesn't have a left function - but you could use substr to generate that effect. Oracle 没有left function - 但您可以使用substr来生成该效果。
  2. Strings in Oracle are concatenated by the || Oracle 中的字符串由||连接。 operator, not +运算符,而不是+
  3. You should convert the username to lowercase:您应该将用户名转换为小写:
SELECT TOLOWER(SUBSTR(firstname, 1, 1) || lastname) AS username
FROM   employee2
select (substr(firstname, 1,1) || lower(lastname)) as UserName 
 from employee2;
select lower(concat(substr(firstname,1,1),lastname)) Username from employee2;

or 

select lower(substr(firstname,1,1)||lastname)  from employee2;

Insert with select

SQL> create table test (id number,uname varchar2(100));

SQL> insert into test
  2  select rownum,lower(concat(substr(first_name,1,1),last_name))||'@gmail.com'
  3  from employees;

108 rows created.


SQL> commit;

Commit complete.


SQL> select * from test
  2  fetch first 5 rows only;

        ID UNAME
---------- ----------------------------------------------------------------------------------------------------
         1 eabel@gmail.com
         2 sande@gmail.com
         3 matkinson@gmail.com
         4 daustin@gmail.com
         5 hbaer@gmail.com


SQL>
MERGE INTO employee 2 
USING (select (substr(firstname, 1,1) || lower(lastname)) as UserName 
 from employee 1) ilv;
ON ilv.username = 2.username
   
    WHEN NOT MATCHED THEN
        INSERT (username)
        values(ilv.username);
UPDATE employee2
SET username = lower(CONCAT(substr(firstname, 1,1),lastname));

Thanks everyone for your help, might have not explained my question correctly but this is the output i wanted to achieve.感谢大家的帮助,可能没有正确解释我的问题,但这是我想要实现的 output。

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

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