格式化SQL查询的结果

时间:2017-11-10 15:15:49

标签: sql oracle

我正在使用以下查询以将'@'替换为' - '并输出如下输出。

查询:

select upper(replace(replace(software_module_component_name,'DBSM:',''),'@','-')) as Test from SOFTWARE_MODULE 
where software_module_component_name like 'DBSM:PUKEIM%'

输出:

Test
PUKEIMA1-GBL05266-ORACLE - V11.2.0.4
PUKEIMA1-GBL05267-IBM - V11.2.0.4
PUKEIMA1-GBL05268-INTEL - V11.2.0.4

但我希望看到输出如下。

要求输出:

Test
PUKEIMA1-GBL05266
PUKEIMA1-GBL05267
PUKEIMA1-GBL05268

此致 Bharath Vikas

1 个答案:

答案 0 :(得分:0)

由于输出看起来是对称的,因此您可以使用SUBSTR并修复SUBSTR字符。请尝试:

 SELECT SUBSTR (
          UPPER (
             REPLACE (REPLACE (software_module_component_name, 'DBSM:', ''),
                      '@',
                      '-')),
          1,
          17)
          AS Test
  FROM SOFTWARE_MODULE
 WHERE software_module_component_name LIKE 'DBSM:PUKEIM%'

你可以这样做:

WITH tbl (str)
     AS (SELECT 'PUKEIMA1-GBL05266-ORACLE - V11.2.0.4' FROM DUAL
         UNION ALL
         SELECT 'PUKEIMA1-GBL05267-IBM - V11.2.0.4' FROM DUAL
         UNION ALL
         SELECT 'PUKEIMA1-GBL05268-INTEL - V11.2.0.4' FROM DUAL)
--Actual query
SELECT    REGEXP_SUBSTR (str,
                         '[^-]+',
                         1,
                         1)
       || REGEXP_SUBSTR (str,
                         '(.*?)([^-]+)',
                         1,
                         2)
          AS
  FROM tbl