简体   繁体   English

Oracle PL / SQL字符串格式

[英]Oracle PL/SQL String Formatting

I started learing Oracle PL/SQL and I downloaded Oracle Database 10g Express with same examples and questions. 我开始学习Oracle PL / SQL,我下载了Oracle Database 10g Express,并提供了相同的示例和问题。

There is a question which I could not solve. 有一个问题我无法解决。

Question is: 问题是:

Write an SQL query to retrieve the first name, last name, and the code of each employee where the code of an employee is found as follows: Firstly remove all occurrences of the characters “i” and “l”, then, concatenate the first six letters of the name, a dash sign “-“, and the last six characters of the last name where only the first and last character of the code should be uppercase. 编写一个SQL查询来检索找到员工代码的每个员工的名字,姓氏和代码,如下所示:首先删除所有出现的字符“i”和“l”,然后连接第一个六个字母的名称,一个破折号“ - ”,以及姓氏的最后六个字符,其中只有代码的第一个和最后一个字符应该是大写的。 If the name does not contain six letters, put underscores (“ ”) to the end of the piece; 如果名称不包含六个字母,请将下划线(“ ”)放在作品的末尾; if the last name does not contain six letters, put underscores (“ ”) to the start of the piece. 如果姓氏不包含六个字母,请将下划线(“ ”) 放在作品的开头。 Order the list according to the last name, and then according to the name. 根据姓氏订购列表,然后根据名称。

OUTPUT MUST BE LIKE THAT 输出必须是这样的

在此输入图像描述

I wrote something but it is totaly wrong and not clear. 我写了一些东西,但这完全错了,不清楚。 Which parts should I fix? 我应该修复哪些部分?


SELECT employees.first_name, employees.last_name,
replace(replace(first_name,'l',''),'i'),
initcap(substr(rpad(employees.first_name,6,'_'),1,6)) || '-' ||

case when length(employees.last_name)>4
then lower(substr(employees.last_name,-5,4))
else lower(substr(lpad(employees.last_name,5,'_'),-5,4)) end ||
upper(substr(employees.last_name,-1,1)) code

FROM employees
ORDER BY last_name, first_name;

This is my output(WRONG) 这是我的输出(错误) 在此输入图像描述

you can write it like this: 你可以这样写:

select first_name, last_name, f
       ||'-'
       ||substr(l, 1, length(l) - 1)
       ||upper(substr(l, -1)) code
  from (select first_name, last_name,
               initcap(rpad(substr(translate(first_name, 'xil', 'x'), 1, 6), 6,
                       '_')) f,
               lpad(substr(translate(last_name, 'xil', 'x'),
                           greatest(-6, -length(translate(last_name, 'xil', 'x')))), 6,
                          '_')
                          l
          from employees); 

i've assumed you only wanted to replace i and l and not also I and L . 我一直以为你只是想取代il不还IL translate will act the same as replace(replace(str, 'l', ''), 'i', '') in this case. 在这种情况下replace(replace(str, 'l', ''), 'i', '') translate将与replace(replace(str, 'l', ''), 'i', '')

This code exactly follows your requirement: Replace the column name and the table name with the desired values 此代码完全符合您的要求:使用所需的值替换列名称和表名称

SELECT ENAME,
       JOB,
          INITCAP (RPAD (REPLACE (REPLACE (ENAME, 'I'), 'i'), 6, '_'))
       || '-'
       || LPAD (
             reverse (
                INITCAP (
                   SUBSTR (reverse ( (REPLACE (REPLACE (JOB, 'I'), 'i'))),
                           1,
                           6))),
             6,
             '_')
          code
  FROM emp 
  ORDER BY JOB, Ename

This code that somewhat follows your original logic: 这段代码有点遵循原始逻辑:

SELECT e."First_Name", e."Last_Name",
       initcap(rpad(replace(replace(e."First_Name", 'l'), 'i'),6,'_'))
       || '-' ||
       reverse(initcap(reverse(lpad(replace(replace(e."Last_Name", 'l'), 'i'),6,'_')))) "Code"
FROM Employees e
ORDER BY e."Last_Name", e."First_Name";

I'm using REVERSE twice so I can use INITCAP for the last name as well. 我正在使用REVERSE两次,所以我也可以使用INITCAP作为姓氏。 I also omitted the 3rd parameter of REPLACE since the function uses empty string by default. 我也省略了REPLACE的第3个参数,因为该函数默认使用空字符串。

Here's a SQL Fiddle DEMO I build from the part of your data. 这是我根据您的数据部分构建的SQL Fiddle DEMO Feel free to add more data. 随意添加更多数据。

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

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