13

我有一个名为的表datas,我正在执行这样的查询:

SELECT linkurl AS DOWNLOADURL,
       lastrevlevel AS VERSION,
       code AS DESCRIPTION,
       created AS RELEASEDATE,
       name AS TYPE
FROM datas
WHERE id IN (SELECT child_id
          FROM   datas _datas
          WHERE  parent_id = (SELECT Max(id)
                              FROM   datas
                              WHERE  code = 'AN4307SW'))

它返回如下结果:

DOWNLOADURL               VERSION DESCRIPTION RELEASEDATE    TYPE
/artifacts/download.txt   2.0     images       25/6/12      download.txt

Type列中,我正在获取文件的名称。我需要获取Type列中文件名的文件扩展名。我怎样才能做到这一点?

例子:

TYPE
.txt
.pdf
.xls
4

6 回答 6

30

您可以使用SUBSTRING_INDEX. 像这样:

select linkurl as DOWNLOADURL,lastrevlevel as VERSION,
code  as DESCRIPTION,created as RELEASEDATE,
SUBSTRING_INDEX(name,'.',-1) as TYPE
from datas where id in
(select child_id from datas _datas 
where parent_id=( select max(id) from datas 
where code = 'AN4307SW'))

编辑

如果您看到有关此功能的文档,我认为这将很好地满足您的要求。

在计数出现分隔符 delim 之前从字符串 str 返回子字符串。如果 count 为正,则返回最终分隔符左侧的所有内容(从左侧开始计数)。如果 count 为负数,则返回最终分隔符右侧(从右侧开始计数)的所有内容。SUBSTRING_INDEX() 在搜索 delim 时执行区分大小写的匹配。

这也将处理这样的情况:

select SUBSTRING_INDEX('Test.Document.doc','.',-1);

编辑2

如果您使用的是 oracle。下次请在正确的事项中标记问题。甲骨文中没有SUBSTRING_INDEX。但我可以看到你可以很容易地做到这一点:

SELECT SUBSTR('Test.Document.doc', INSTR('Test.Document.doc', '.',-1)) 
FROM dual; 

像这样的完整查询:

select linkurl as DOWNLOADURL,lastrevlevel as VERSION,
    code  as DESCRIPTION,created as RELEASEDATE,
    SUBSTR(name, INSTR(name, '.',-1))  as TYPE
    from datas where id in
    (select child_id from datas _datas 
    where parent_id=( select max(id) from datas 
    where code = 'AN4307SW'))

参考这里

于 2012-06-08T08:45:28.760 回答
2
select linkurl as DOWNLOADURL,lastrevlevel as VERSION,
code  as DESCRIPTION,created as RELEASEDATE,reverse(substring(reverse(name), 1,charindex('.', reverse(name))-1)) as TYPE
from datas where id in
(select child_id from datas _datas 
where parent_id=( select max(id) from datas 
where code = 'AN4307SW'))  
于 2012-06-08T08:48:09.087 回答
1

认为你需要这样的东西

SELECT REPLACE(name,SUBSTRING(name ,0, CHARINDEX('.', name )),'')
于 2012-06-08T08:57:18.473 回答
1
SELECT REVERSE(SUBSTRING(REVERSE(name),1,LOCATE('.',REVERSE(name),1‌​)));
于 2017-10-18T12:01:36.650 回答
1

可以这样做

SELECT SUBSTRING_INDEX(FILE_NAME,"." ,-1) from TABLE_NAME
于 2019-06-30T04:59:51.393 回答
0
SELECT
    SUBSTRING(file_name,(LENGTH(file_name)-LOCATE('.',REVERSE(file_name)))+2)
FROM <table name> WHERE file_id=<file_id>;
于 2015-09-08T14:23:19.023 回答