如何从左侧选择字符,不包括右侧的.txt

时间:2017-12-24 11:51:26

标签: database oracle

该列的数据不统一,有些数据末尾有“.txt”。 将其中一个条目视为“ABCDEFG.TXT” 我想让我的select语句给我“ABCDEFG”。

4 个答案:

答案 0 :(得分:0)

以下查询将非常有用

    Home: {screen : Home,
    navigationOptions: {
      headerLeft: null,
      title: 'Home',
      headerTitleStyle: {fontFamily: 'myFont', fontSize: 20,},
      headerStyle: { marginTop: 24, },
    },
  },

输出继电器:

DECLARE @Var NVARCHAR(50)
SET @Var = 'ABCDASDFASF.txt'

SELECT LEFT(@Var, CHARINDEX('.', @Var)-1) 'Filename' 

答案 1 :(得分:0)

func splitOnAlternateNewline(line: String) -> [String]{
    var returnArray: [String] = []
    var aString = ""
    outLoop :for char in line{
        let aChar = String(char).utf16
        for a in aChar{
            if a == 8232{
                returnArray.append(aString)
                aString = ""
                continue outLoop

            }

        }
        aString.append(char)


    }
    returnArray.append(aString)
    return returnArray

} 

或者,如果您需要它不区分大小写:

with demo (text) as
     ( select 'ABCDEFG.TXT' from dual union all
       select 'HIJKL' from dual )
select text
     , regexp_replace(text, '\.TXT$')
from   demo;

TEXT         REGEXP_REPLACE(TEXT,'\.TXT$')
------------ --------------------------------------------
ABCDEFG.TXT  ABCDEFG
HIJKL        HIJKL

或者删除任何三个字符的文件扩展名:

with demo (text) as
     ( select 'ABCDEFG.TXT' from dual union all
       select 'abcdefg.txt' from dual union all
       select 'HIJKL' from dual )
select text
     , regexp_replace(text, '\.TXT$', '', 1, 1, 'i') as replaced
from   demo;

TEXT         REPLACED
------------ --------------------------------------------
ABCDEFG.TXT  ABCDEFG
abcdefg.txt  abcdefg
HIJKL        HIJKL

或者删除任何长度的任何扩展名:

regexp_replace(text, '\....$', '', 1, 1, 'i')

答案 2 :(得分:0)

您可以使用//Returns the first duplicate element function firstDup(arr) { let o = {}; //an empty object for (let i = 0; i < arr.length; i++) { if (o[arr[i]]) { //check if the property exists return arr[i]; } else { o[arr[i]] = 'a'; //set the object's property to something non falsy } } return -1; //If No duplicate found return -1 } console.log(firstDup([2, 3, 3, 1, 5, 2])); console.log(firstDup([2, 4, 3, 5, 1]));SUBSTR的组合,它比INSTR函数更有效。

即使您的文字中有多个点REGEXP_,此功能仍然有效。它搜索最后一个点。如果您需要考虑所有类型的扩展程序,请搜索"."而不是'.'。然后,您应该搜索'.TXT'作为一般情况。

CASE WHEN txt LIKE '%.%'

DEMO

答案 3 :(得分:0)

解决方案:

select
    case
        when upper(file_name) like '%.TXT' then substr(file_name, 1, (length(file_name) - 4))
        else file_name
    end  cleaned_file_name
from
    file_names;

测试:

create table file_names
(
    file_name     varchar2(30)
);

insert into file_names (file_name) values ('ABCDEFG');
insert into file_names (file_name) values ('ABCDEFG.TXT');
insert into file_names (file_name) values ('ABCDEFGTXT');
insert into file_names (file_name) values ('ABCD.TXT.EFG');
insert into file_names (file_name) values ('abcdefg.txt');
insert into file_names (file_name) values ('ABCDEFG.DOC');

select
    case
        when upper(file_name) like '%.TXT' then substr(file_name, 1, (length(file_name) - 4))
        else file_name
    end  cleaned_file_name
from
    file_names;

CLEANED_FILE_NAME
--------------------------------------------------------------------------------
ABCDEFG
ABCDEFG
ABCDEFGTXT
ABCD.TXT.EFG
abcdefg
ABCDEFG.DOC

6 rows selected.