17

I have a table named datas and I'm executing a query like this:

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'))

It returns results like this:

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

In the Type column I am geting name of the file. I need to get the file extension of the file name in the Type column. How can I achieve this?

Examples:

TYPE
.txt
.pdf
.xls
hakre
  • 193,403
  • 52
  • 435
  • 836
anto
  • 315
  • 3
  • 9
  • 18
  • 2
    What would you expect to happen if I had a file named `Test.Document.doc` ? What about if no extension was specified? – Bridge Jun 08 '12 at 08:44
  • @bridge i want only extension of file name – anto Jun 08 '12 at 09:10
  • 1
    @anto So the substring after the last dot - in my example `doc`. Is it possible that there could be a file without an extension (not containing a dot at all?) – Bridge Jun 08 '12 at 09:18
  • @bridge: yes there is a chance of not getting dot, then how should i proceed – anto Jun 08 '12 at 09:23
  • @anto In that case, the accepted answer which uses `SUBSTRING_INDEX` will return the whole name of the file if there is no dot. Is that acceptable? – Bridge Jun 08 '12 at 09:41
  • @Bridge: but i am having certain other fields which would definitely have extension at last. – anto Jun 08 '12 at 09:59

6 Answers6

34

You can use SUBSTRING_INDEX. Like this:

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'))

EDIT

If you see the docs on this function I think this will apply well to you requirements.

Returns the substring from string str before count occurrences of the delimiter delim. If count is positive, everything to the left of the final delimiter (counting from the left) is returned. If count is negative, everything to the right of the final delimiter (counting from the right) is returned. SUBSTRING_INDEX() performs a case-sensitive match when searching for delim.

This will also handle a case like this:

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

EDIT2

If you are using oracle. Please tag the question in the correct matter next time. There is no SUBSTRING_INDEX in oracle. But what I can see you can do this quite easy:

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

Full query like this:

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'))

Reference here

Community
  • 1
  • 1
Arion
  • 31,011
  • 10
  • 70
  • 88
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'))  
Alpesh Prajapati
  • 1,593
  • 2
  • 18
  • 38
1

think you'll need something like this

SELECT REPLACE(name,SUBSTRING(name ,0, CHARINDEX('.', name )),'')
Rohan Büchner
  • 5,333
  • 4
  • 62
  • 106
  • select REVERSE(SUBSTRING(REVERSE(name),1,LOCATE('.',REVERSE(name),1))) – mansi Oct 18 '17 at 12:01
  • I want to post it as another ans but its get posted as comment here wrongly. but how do i delete this comment ? there is no option to delete – mansi Oct 23 '17 at 10:08
1
SELECT
    SUBSTRING(file_name,(LENGTH(file_name)-LOCATE('.',REVERSE(file_name)))+2)
FROM <table name> WHERE file_id=<file_id>;
Sjon
  • 4,989
  • 6
  • 28
  • 46
Sebin
  • 11
  • 1
1
SELECT REVERSE(SUBSTRING(REVERSE(name),1,LOCATE('.',REVERSE(name),1‌​)));
Rohan Büchner
  • 5,333
  • 4
  • 62
  • 106
mansi
  • 837
  • 5
  • 12
1

It can be done like this

SELECT SUBSTRING_INDEX(FILE_NAME,"." ,-1) from TABLE_NAME